将#Format格式添加到值

时间:2015-01-26 23:49:40

标签: excel vba excel-vba number-formatting

我有一个简单的表单,我有一个用户输入某些数据。有了这些数据,我需要计算一些值,我还需要将值作为货币或百分比。

代码是:

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.ValueAnswer& Answer2以数字出现。我需要txtActiveAff.ValuetxtConvRate.Value为%。 txtAOV.valueAnswer 3应为货币。

我尝试在每张纸后添加。(“投资回报率”)a .NumberFormat= "Percentage"但是没有用。
当我使用表格时,我也没有得到正确的答案,因为计算字段为0。任何想法?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

以下是我最终使用的内容,以防它帮助别人!

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