根据选项按钮选择将文本框正值转换为负值
Convert Textbox Positive Values to Negative Based on Option Button Selection
我正在开发一个 Excel 用户表单,其中包含一个文本框,供用户输入收到或提供的美元金额。选择命令按钮后,该数字将放入电子表格单元格中。我有两个单选按钮,一个是给钱,一个是收钱。
我希望用户在框中输入一个正数,当给定的按钮被选中时,它会变成一个负数。选择接收按钮后,它会将框保留为正。
通常我会将某个值乘以 -1 使其变为负数。我试过了 userform.textbox.value = userform.textbox.value * -1
.
Private Sub InsTxtBox_AfterUpdate()
GameLogUF.InsTxtBox = Format(GameLogUF.InsTxtBox, "$#,###")
If GameLogUF.InsPlyrOptnBtn.Value = True Then
GameLogUF.InsTxtBox.Value = GameLogUF.InsTxtBox.Value * -1
End If
End Sub
首先,检查您是否触发了事件。在此子中放置断点并尝试更改值。
更新控件或记录时触发AfterUpdate事件。在一条记录中,当 控件失去焦点 或当用户 按下 Enter 或 Tab。[=11= 时,每个控件中更改的数据都会更新]
刚刚检查了您的代码,如果事件被触发,它就可以正常工作。
另外,我建议稍微修改一下。
Dim value As Double
Dim tryValue As String
'remove $ sign so String value could be converted to Number format
tryValue = Replace(GameLogUF.InsTxtBox.value, "$", "")
'if user input is not numeric after removing $ sign - input incorrect
If Not IsNumeric(tryValue) Then
'do sth else
Exit Sub
End If
value = tryValue
' "= True" part is not needed there is no need to compare boolean property '
If GameLogUF.InsPlyrOptnBtn.value Then
'if user input is negative your code will turn it back to positive, with Abs it always will be negative in this case'
value = Abs(value) * -1
End If
'do formatting in the end'
GameLogUF.InsTxtBox.value = Format(value, "$#,###")
我正在开发一个 Excel 用户表单,其中包含一个文本框,供用户输入收到或提供的美元金额。选择命令按钮后,该数字将放入电子表格单元格中。我有两个单选按钮,一个是给钱,一个是收钱。
我希望用户在框中输入一个正数,当给定的按钮被选中时,它会变成一个负数。选择接收按钮后,它会将框保留为正。
通常我会将某个值乘以 -1 使其变为负数。我试过了 userform.textbox.value = userform.textbox.value * -1
.
Private Sub InsTxtBox_AfterUpdate()
GameLogUF.InsTxtBox = Format(GameLogUF.InsTxtBox, "$#,###")
If GameLogUF.InsPlyrOptnBtn.Value = True Then
GameLogUF.InsTxtBox.Value = GameLogUF.InsTxtBox.Value * -1
End If
End Sub
首先,检查您是否触发了事件。在此子中放置断点并尝试更改值。
更新控件或记录时触发AfterUpdate事件。在一条记录中,当 控件失去焦点 或当用户 按下 Enter 或 Tab。[=11= 时,每个控件中更改的数据都会更新]
刚刚检查了您的代码,如果事件被触发,它就可以正常工作。
另外,我建议稍微修改一下。
Dim value As Double
Dim tryValue As String
'remove $ sign so String value could be converted to Number format
tryValue = Replace(GameLogUF.InsTxtBox.value, "$", "")
'if user input is not numeric after removing $ sign - input incorrect
If Not IsNumeric(tryValue) Then
'do sth else
Exit Sub
End If
value = tryValue
' "= True" part is not needed there is no need to compare boolean property '
If GameLogUF.InsPlyrOptnBtn.value Then
'if user input is negative your code will turn it back to positive, with Abs it always will be negative in this case'
value = Abs(value) * -1
End If
'do formatting in the end'
GameLogUF.InsTxtBox.value = Format(value, "$#,###")