Left command in VBA

tpthatsme

New Member
Joined
Jun 2, 2016
Messages
17
Hello Mr. Excel community,

I have a small project that seems to be beating me and I need your help. I have a column of data that could be letters and numbers or all numbers (PartNo). I would like to change the value to the first 5 characters (Base5). I can do this as a formula in Excel, but my VBA isn't yielding the same results with long numbers. If the data is a long number (ex. 20 digits long), my VBA returns a Left 5 of an exponent number (ex. VBA: 65181651626513100000 returns 6.518 and formula returns 65181). I really need to keep this in VBA because I will be running this macro on new files daily.

I am not sure if my VBA is the most efficient, so any help would be great with this as well.

Code:
Sub Base5()

Dim Base5 As Range
Dim PartNo As Range
Dim i As Long
Dim AllRecords As Long




Set Base5 = Sheet1.Range("U2")
Set PartNo = Sheet1.Range("L2")
AllRecords = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To AllRecords
    Cells(i, "U").Value = Left(Cells(i, "L").Value, 5)
Next i
'Column title: This tells us when the macro is done.
Range("U1").Value = "Base5"
End Sub

Thanks for your help!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
try
Code:
Cells(i, "U").Value = Left(Format(Cells(i, "L").Value, "0.0"), 5)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,550
Members
449,237
Latest member
Chase S

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