简体   繁体   中英

Excel VBA how to treat value as a integer rather than as a string

I am trying to get my script to treat my value as a interger rather than a string. It is a vlookup, that gets the date from a formatted number cell. When I put the data into the sheet though, it acts as a string and not a number. Here is my code:

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

How can I set the Quantity as a integer and the Price each and Total price as a currency?

Thanks in advance.

Try changing

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

And adding

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

what kind of string is being returned and what kind of integer do you need ?

You may want either the logical number the points to the date or the date number like 20150224

If the string being returned is something like "20150224" the it suffices to use the CInt() function as in

my_date_int=CInt(dateString)

But if it is returning a string like "2015-02-24" and you want a integer 20150224 you can use Format() as in

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

please in the future be clearer about what is the input you're getting and what is the output that you desire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM