将 # 格式添加到值

Adding # Format to Values

我有一个简单的表单,用户可以在其中输入某些数据。有了这些数据,我需要计算一些值,我还需要这些值以货币或百分比的形式出现。

密码是:

Private Sub cmdAdd_Click()
    Dim emptyRow As Long
    Dim ctl As Control
    Dim A As Long
    Dim B As Long
    Dim C As Long
    Dim D As Long
    Dim E As Long
    Dim Answer As Long
    Dim Answer2 As Long
    Dim Answer3 As Long

    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 = Sheets("ROI").Range("A" & Rows.Count).End(xlUp).Row + 1
    A = CLng(txtNoTotalAff.Value)
    B = CLng(txtActiveAff.Value)
    C = CLng(txtAvgTraffic.Value)
    D = CLng(txtConvRate.Value)
    E = CLng(txtAOV.Value)

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

    'Transfer Information
    Sheets("ROI").Cells(emptyRow, 1) = lstClientName.Value     'Col "A"
    Sheets("ROI").Cells(emptyRow, 2) = txtNoTotalAff.Value     'Col "B"
    Sheets("ROI").Cells(emptyRow, 3) = txtActiveAff.Value      'Col "C"
    Sheets("ROI").Cells(emptyRow, 4) = Answer                 'Col "D"
    Sheets("ROI").Cells(emptyRow, 5) = txtAvgTraffic.Value     'Col "E"
    Sheets("ROI").Cells(emptyRow, 6) = Answer2                'Col "F"
    Sheets("ROI").Cells(emptyRow, 7) = txtConvRate.Value      'Col "G"
    Sheets("ROI").Cells(emptyRow, 8) = txtAOV.Value            '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

我需要 txtNoTotalAff.ValueAvgTraffic.ValueAnswerAnswer2 作为数字。我需要 txtActiveAff.ValuetxtConvRate.Value 为 %。 txtAOV.valueAnswer 3 应该是货币。

我尝试在每张纸之后添加。("ROI") .NumberFormat= "Percentage" 但那没有用。
当我使用表单时,我也没有得到正确的答案,因为计算字段显示为 0。有什么想法吗?

试试这个

Private Sub cmdAdd_Click()
    Dim emptyRow As Long
    Dim ctl As Control
    'Do not convert to long type as in calculation will be long also, 
    'e.g. Clng(2.3)*Clng(3.6) result will be 8 instead of 8.28,
    'for calculation use decimal or single or double depending on required accuracy 
    'to get decimal do not specify data type
    'data type is variant for the variables (A, B, C, D, E, Answer,Answer2, Answer3)
    'but when you assign data like Cdec(value), variables will be converted to decimal
    Dim A, B, C, D, E, Answer, Answer2, Answer3
    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 = Sheets("ROI").Range("A" & Rows.Count).END(xlUp).Row + 1
    A = CDec(txtNoTotalAff.Value) 'convert to decimal, not to long
    B = CDec(txtActiveAff.Value) 'convert to decimal, not to long
    C = CDec(txtAvgTraffic.Value) 'convert to decimal, not to long
    D = CDec(txtConvRate.Value) 'convert to decimal, not to long
    E = CDec(txtAOV.Value)
    Answer = A * B: Answer2 = Answer * C: Answer3 = (Answer2 * D) * E
    'Transfer Information
    With Sheets("ROI")
        'Use this here, depending on which format of the cell you need
        '.Cells(Row, Column).NumberFormat = "0.00%"
        '.Cells(Row, Column).NumberFormat = "[$$-409]#,##0.00"
        .Cells(emptyRow, 1) = lstClientName.Value    'Col "A"
        .Cells(emptyRow, 2) = txtNoTotalAff.Value    'Col "B"
        .Cells(emptyRow, 3) = txtActiveAff.Value     'Col "C"
        .Cells(emptyRow, 4) = Answer                 'Col "D"
        .Cells(emptyRow, 5) = txtAvgTraffic.Value    'Col "E"
        .Cells(emptyRow, 6) = Answer2                'Col "F"
        .Cells(emptyRow, 7) = txtConvRate.Value      'Col "G"
        .Cells(emptyRow, 8) = txtAOV.Value           'Col "H"
        .Cells(emptyRow, 9) = Answer3                'Col "I"
        .Cells(emptyRow, 10) = txtAffName.Value      'Col "J"
    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

这是我最终使用的,以防它能帮助到其他人!

Private Sub cmdAdd_Click()

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


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 = Sheets("ROI").Range("A" & Rows.Count).End(xlUp).Row + 1
A = CDbl(txtNoTotalAff.Value)
B = CDbl(txtActiveAff.Value)
C = CDbl(txtAvgTraffic.Value)
D = CDbl(txtConvRate.Value)
E = CDbl(txtAOV.Value)

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

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

MsgBox "Total Sales: $" & Answer3, vbExclamation, "The Affiliate will need to          generate!"

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