0

所以我有这段代码,如果我选中一个复选框,它会将值添加到总计中,但如果我取消选中它,它不会减去该值

Private Sub CheckBox11_Click()

Dim ws As Worksheet
Dim wf As WorksheetFunction
Dim sValue As String
Dim sAdd As Boolean

Set wf = Application.WorksheetFunction
Set ws = Sheet2

If CheckBox11.Value = True Then
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    sValue = wf.VLookup(ComboBox1.Value, ws.Range("A4:R" & LastRow), 13, False)
    
    TextBox11.Value = sValue & " €"
    sAdd = True
Else
    TextBox11.Value = ""
    sValue = "0"
    sAdd = False
End If

Call TotalValue(sValue, sAdd)

End Sub

总值函数:

  Function TotalValue(BoxValue As String, sAdd As Boolean, Optional Desc As String) As String

Dim auxDesc As Double
Dim auxValue As Double

If UserForm1.TextBox17.Value = "0" Then

    If sAdd = True Then
        UserForm1.TextBox17.Value = BoxValue
    End If
    
Else

    If sAdd = True Then
        UserForm1.TextBox17.Value = CStr(CDec(UserForm1.TextBox17.Value) + CDec(BoxValue))
    Else
        UserForm1.TextBox17.Value = CStr(CDec(UserForm1.TextBox17.Value) - CDec(BoxValue))
    End If
    
End If

If Desc <> "" Then
    auxDesc = Left(Desc, Len(Desc) - 1) / 100
    auxValue = CDec(UserForm1.TextBox17.Value) * auxDesc

    UserForm1.TextBox17.Value = CStr(CDec(UserForm1.TextBox17.Value) - auxValue)
    
End If
    
End Function

我想要的是如果我取消选中该框以减去总值,如果我选中它会在 total 上添加值,如果我取消选中它保持“添加”不会减去:(

有什么帮助吗?

4

0 回答 0