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

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

提前谢谢。

尝试更改

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

并添加

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

返回的字符串是什么类型的,需要什么类型的整数?

您可能想要指向日期的逻辑数字或类似20150224 的日期数字

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

my_date_int=CInt(dateString)

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

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

请在未来更清楚地知道你得到的是什么,你想要的是什么。

最新更新