根据选项按钮的选择将文本框的正值转换为负数

问题描述 投票:0回答:1

我正在使用带有文本框的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
excel vba textbox userform
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.