我如何获取货币标签的价值?

时间:2014-02-20 06:30:06

标签: vba currency

我正在尝试在列表框中添加标签,然后使用val()将它们加在一起。但是,当我向标签添加货币符号时,val()停止工作。

这是不使用任何货币符号的代码:

'Sales Tax
Public Const intSALESTAX_Rate As Decimal = 0.06

'Shipping cost
Public Const Shipping_Rate As Integer = 2

'Audio Books
Public Const History_of_Scotland_Audio As Decimal = 14.5
Public Const Calculus_One_Day_Audio As Decimal = 29.95
Public Const Science_Body_Language_Audio As Decimal = 12.95
Public Const Relaxation_Techniques_Audio As Decimal = 11.5

'Print Books
Public Const Did_Your_Way_Print As Decimal = 11.95
Public Const History_Of_Scotland_Print As Decimal = 14.5
Public Const Calculus_One_Day_Print As Decimal = 29.95
Public Const Feel_The_Stress_Print As Decimal = 18.5

Private Sub btnAddtoCartAudioBook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddtoCartAudioBook.Click
    Dim value As Decimal
    Dim shipping As Integer
    Dim tax As Decimal
    Dim grand_total As Decimal
    Dim ShoppingCart As New Form1()

    If lstAudioBooks.SelectedIndex = -1 Then
        MessageBox.Show("Select an audio book.")
        Return
    End If

    Form1.lstShoppingCart.Items.Add(lstAudioBooks.SelectedItem.ToString())

    Select Case lstAudioBooks.SelectedItem.ToString()

        Case "Learn Calculus in One Day (Audio)"
            Debug.WriteLine("Learn Calculus in One Day (Audio)")
            value = Val(Form1.lblSubtotal.Text) + Form1.Calculus_One_Day_Audio
            Form1.lblSubtotal.Text = FormatNumber(value, 2)
        Case "Relaxation Techniques (Audio)"
            Debug.WriteLine("Relaxation Techniques (Audio)")
            value = Val(Form1.lblSubtotal.Text) + Form1.Relaxation_Techniques_Audio
            Form1.lblSubtotal.Text = FormatNumber(value, 2)
        Case "The History of Scotland (Audio)"
            Debug.WriteLine("The History of Scotland (Audio)")
            value = Val(Form1.lblSubtotal.Text) + Form1.History_of_Scotland_Audio
            Form1.lblSubtotal.Text = FormatNumber(value, 2)
        Case "The Science of Body Language (Audio)"
            Debug.WriteLine("The Science of Body Language (Audio)")
            value = Val(Form1.lblSubtotal.Text) + Form1.Science_Body_Language_Audio
            Form1.lblSubtotal.Text = FormatNumber(value, 2)
        Case Else
            Debug.WriteLine("No Value")
    End Select

    'shipping calculations
    shipping = Val(Form1.lblShipping.Text) + Form1.Shipping_Rate
    Form1.lblShipping.Text = FormatNumber(shipping, 2)

    'tax calculations
    tax = value * Form1.intSALESTAX_Rate
    Form1.lblTax.Text = FormatNumber(tax, 2)

    'Sub total calculation
    grand_total = FormatNumber(value + shipping + tax, 2)
    Form1.lblTotal.Text = FormatNumber(grand_total, 2)

    Return
End Sub

但是,如果我在标签上添加一个美元符号,则val()停止工作

value = Val(Form1.lblSubtotal.Text) + Form1.Calculus_One_Day_Audio Form1.lblSubtotal.Text = "$" + FormatNumber(value, 2)

如果值包含美元符号,如何使用标签运行计算?

1 个答案:

答案 0 :(得分:0)

一种方法是在调用val()函数之前删除美元符号。例如,假设数字采用以下格式: 45美元,1美元,456美元

dim strTemp as string
strTemp = strings.right(strings.len(Form1.lblSubtotal.Text)-1)
Value = Val(strTemp)

如果它的格式为: 45美元,1美元,456美元

dim strTemp as string
strTemp = strings.right(strings.len(Form1.lblSubtotal.Text)-2)
Value = Val(strTemp)