Converting a string into an integer using VBA code

sophl4

New Member
Joined
Jul 8, 2011
Messages
4
Hi,

I'm trying to convert the string "1234" into an integer using VBA code. I know it can be converted on a worksheet by using the Value function. What would be the equivalent of this in VBA? I'm having trouble finding the code for it.

Thanks in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Use the CLng function to convert it. Something like this...

Code:
YourStringNumber = "1234"
ConvertedToIntegerValue = CLng(YourStringNumber)
Another possibility (assuming your number is always a whole number) is this...
Code:
YourStringNumber = "1234"
ConvertedToIntegerValue = Val(YourStringNumber)
 
Last edited:
Upvote 0
Try like this

Code:
Sub atest()
Const s As String = "1234"
Dim i As Integer
i = CInt(s)
MsgBox i
End Sub
 
Upvote 0
Hi sophl4

Remark: if you assign the value to a variable the conversion is automatic, according to the variable type.

In the example the Long variable will get the value 1235 (integer rounded value) and the Double gets 1234.5678.


Code:
Sub Test()
Const s As String = "1234.5678"
Dim l As Long
Dim d As Double
 
l = s
d = s
 
'...
 
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,510
Members
452,918
Latest member
Davion615

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top