Excel VBA如何将值视为整数而不是字符串

时间:2015-02-24 18:51:01

标签: string excel vba

我试图让我的脚本将我的值视为一个整数而不是一个字符串。它是一个vlookup,它从格式化的数字单元格中获取日期。当我将数据放入工作表时,它充当字符串而不是数字。这是我的代码:

Private Sub CommandButton1_Click()

Dim NextProd As Long
Dim NextPC As Long
Dim NextQuant As Long
Dim NextPE As Long
Dim NextTP As Long

NextProd = Cells(Rows.Count, "A").End(xlUp).Row + 1
NextPC = Cells(Rows.Count, "B").End(xlUp).Row + 1
NextQuant = Cells(Rows.Count, "C").End(xlUp).Row + 1
NextPE = Cells(Rows.Count, "D").End(xlUp).Row + 1
NextTP = Cells(Rows.Count, "E").End(xlUp).Row + 1

Dim lookupRange As Range
Set lookupRange = Worksheets("Products").Range("A1:C1679")
Dim Products As Range
Set Products = Worksheets("Products").Range("A1:B1679")
Dim Description As Range
Set Descrpition = Worksheets("products").Range("A1:A1679")

ItemPrice = Application.VLookup(InvoiceProductEntry.Selectprodcutcombo.Value, lookupRange, 3, False)
ProductCode = Application.VLookup(InvoiceProductEntry.Selectprodcutcombo.Value, lookupRange, 2, False)
ProductDescription = Application.VLookup(InvoiceProductEntry.Selectprodcutcombo.Value, lookupRange, 1, False)

Totalprice = TextBox1.Value * ItemPrice

Cells(NextProd, 1) = Selectprodcutcombo.Value
Cells(NextPC, 2) = ProductCode
Cells(NextQuant, 3) = TextBox1.Text
Cells(NextPE, 4) = ItemPrice
Cells(NextTP, 5) = Totalprice

End Sub

如何将数量设置为整数,将每个价格和总价格设置为货币?

提前致谢。

2 个答案:

答案 0 :(得分:0)

尝试更改

Cells(NextQuant, 3) = clng(TextBox1.Text)

并添加

Cells(NextPE, 4).NumberFormat = "$#,##0.00"
Cells(NextTP, 5).NumberFormat = "$#,##0.00"

答案 1 :(得分:-1)

返回什么类型的字符串以及您需要什么样的整数?

您可能需要逻辑编号指向日期的点或日期编号,如20150224

如果返回的字符串类似于"20150224",那么使用CInt()函数就足够了

my_date_int=CInt(dateString)

但如果它返回的字符串如"2015-02-24"而您想要一个整数20150224,则可以使用Format(),如

my_date_int=CInt(Format(dateString,"yyyyMMdd"))

请将来更清楚地了解您所获得的输入是什么以及您想要的输出是什么。