【问题标题】:Convert Textbox Positive Values to Negative Based on Option Button Selection根据选项按钮选择将文本框正值转换为负值
【发布时间】:2020-04-14 11:28:17
【问题描述】:

我正在开发一个带有文本框的 Excel 用户表单,供用户输入收到或给予的美元金额。选择命令按钮后,该数字将放入电子表格单元格中。我有两个单选按钮,一个是给钱的,另一个是收钱的。

I would like the user to enter a positive amount in the box and when the given button is selected it turns into a negative.当接收到的按钮被选中时,它将使该框保持为正。

通常我会乘以 -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

【问题讨论】:

    标签: excel vba textbox userform


    【解决方案1】:

    首先,检查是否触发了事件。在这个 sub 中放置一个断点并尝试更改值。

    AfterUpdate 事件在控件或记录更新时触发。在一条记录中,当控件失去焦点或用户按 Enter 或 Tab 时,每个控件中更改的数据都会更新。

    刚刚检查了你的代码,如果事件被触发,它就可以工作。

    另外,我建议稍微改变一下。

    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, "$#,###")
    

    【讨论】:

    • 非常感谢您的帮助。它工作得很好,除了我确实需要布尔属性来触发它。我应该解释说,一个选项框用于资金流出,另一个用于资金流入,因此如果选择了用于资金流出的选项框,它会将数字变为负数。虽然代码很完美。非常感谢您的帮助,我被难住了。
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    相关资源
    最近更新 更多