与VBA相乘

时间:2015-01-07 23:18:32

标签: variables excel-vba calculated-field vba excel

我有一个表单,我需要将3个不同的值相乘以提供所需的结果。它在下面:

Private Sub cmdAdd_Click()

Dim emptyRow As Long
Dim ctl As Control
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim E As Integer
Dim Answer As Integer
Dim Answer2 As Integer
Dim Answer3 As Integer



If Me.txtNoTotalAff.Value = "" Then
    MsgBox "Please enter the Total Number of Affiliates", vbExclamation, "ROI"
    Me.txtNoTotalAff.SetFocus
    Exit Sub
End If
If Me.lstClientName.Value = "" Then
    MsgBox "Please enter Client Name", vbExclamation, "ROI"
    Me.lstClientName.SetFocus
    Exit Sub
End If
'Determine Empty Row
 emptyRow = Worksheets("ROI").Range("A1").CurrentRegion.Rows.Count
 A = txtNoTotalAff.Value
 B = txtActiveAff.Value
 C = txtAvgTraffic.Value
 D = txtConvRate.Value
 E = txtAOV.Value

 Answer = A * B
 Answer2 = Answer * C
 Answer3 = (Answer2 * D) * E

 'Transfer Information
 With Worksheets("ROI").Range("A1")
.Offset(emptyRow, 0).Value = lstClientName.Value
.Offset(emptyRow, 1).Value = txtNoTotalAff.Value
.Offset(emptyRow, 2).Value = txtActiveAff.Value
.Offset(emptyRow, 3).Value = Answer
.Offset(emptyRow, 4).Value = txtAvgTraffic.Value
.Offset(emptyRow, 5).Value = Answer2
.Offset(emptyRow, 6).Value = txtConvRate.Value
.Offset(emptyRow, 7).Value = txtAOV.Value
.Offset(emptyRow, 8).Value = Answer3
.Offset(emptyRow, 9).Value = txtAffName.Value

 End With

'Clear the Form
For Each ctl In Me.Controls
    If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
    ctl.Value = ""
    ElseIf TypeName(ctl) = "CheckBox" Then
    ctl.Value = False
    End If
Next ctl
 End Sub

我需要使用变量并让它们计算每个变量,或者我想使用答案本身来获得所需的结果。

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我假设错误是因为您试图将恰好是数字字符的文本字符串相乘。他们需要转换为数字。我会使用Long类型或Double,具体取决于您使用的是哪种数字。在文本框中,整数更有可能出现错误,用户输入不同。我将在示例中使用Long,因为它最接近Integer,但如果数字变得很大,或者使用小数,我会将它们切换为Double。

我修改了一些有关您代码的其他内容。主要是获取文本框的.Text属性并将它们转换为Long变量。此外,在地址声明中,不要使用A1中的Offset,而只需使用' .Cells(Row,Col)'来声明地址。一般来说,你的逻辑似乎是对的。

试试这个,看看是否有任何错误。

<强>代码:

Private Sub cmdAdd_Click()

Dim emptyRow As Long
Dim ctl As Control
Dim A As Long, B As Long, C As Long, D As Long, E As Long
Dim Answer As Long, Answer2 As Long, Answer3 As Long

    'Validation
    If txtNoTotalAff.Text= "" Then
        MsgBox ("Please enter the Total Number of Affiliates", vbExclamation, "ROI")
        txtNoTotalAff.SetFocus
        Exit Sub
    End If

    If Me.lstClientName.Value = "" Then
        MsgBox ("Please enter Client Name", vbExclamation, "ROI")
        lstClientName.SetFocus
        Exit Sub
    End If

    'Determine Empty Row
    emptyRow = Sheets("ROI").Range("A" & Rows.count).End(xlUp).row + 1
    A = CLng(txtNoTotalAff.Text)
    B = CLng(txtActiveAff.Text)
    C = CLng(txtAvgTraffic.Text)
    D = CLng(txtConvRate.Text)
    E = CLng(txtAOV.Text)

    Answer = A * B
    Answer2 = Answer * C
    Answer3 = (Answer2 * D) * E

    'Transfer Information
    Sheets("ROI").Cells(emptyRow, 1) = lstClientName.Text     'Col "A"
    Sheets("ROI").Cells(emptyRow, 2 ) = txtNoTotalAff.Text    'Col "B"
    Sheets("ROI").Cells(emptyRow, 3) = txtActiveAff.Text      'Col "C"
    Sheets("ROI").Cells(emptyRow, 4) = Answer                 'Col "D"
    Sheets("ROI").Cells(emptyRow, 5) = txtAvgTraffic.Text     'Col "E"
    Sheets("ROI").Cells(emptyRow, 6) = Answer2                'Col "F"
    Sheets("ROI").Cells(emptyRow, 7) = txtConvRate.Text       'Col "G"
    Sheets("ROI").Cells(emptyRow, 8) = txtAOV.Text            'Col "H"
    Sheets("ROI").Cells(emptyRow, 9) = Answer3                'Col "I"
    Sheets("ROI").Cells(emptyRow, 10) = txtAffName.Value      'Col "J"

    'Clear the Form
    For Each ctl In Me.Controls
        If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
        ctl.Value = ""
        ElseIf TypeName(ctl) = "CheckBox" Then
        ctl.Value = False
        End If
    Next ctl
End Sub