Excel / VBA Left Function (Fine-tuning)

Sambora

New Member
Joined
Feb 5, 2013
Messages
10
I'm trying to extract the first three characters in a cell and have found this which works for numbers and text apart from where the cell begins with a 0 or multiple 0's

Sub CompareScheme()
Dim SourceRange As Range
Dim DestinationRange As Range
Dim Z As Integer

Set SourceRange = Range("A1:A10")
Set DestinationRange = Range("D1:D10")

For Z = 1 To SourceRange.Count
DestinationRange(Z, 1).Value = Left$(SourceRange(Z, 1).Value, 3)
Next Z
End Sub


For example, if a cell is 0045845, I want it to bring back 004 and not just 4. I thought if I changed the = Left$(SourceRange(Z, 1).Value, 3) to = Left$(SourceRange(Z, 1).Text, 3) then this might solve it but it makes no difference to column D. I've almost got it how I want it, but unless I have Column D in the right format, I can't do all of the analysis that I would want.

Any help would be greatly appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try adding this line of code right before your For loop...
Code:
DestinationRange.NumberFormat = "@"
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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