Left function VBA

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, something strange is causing my LEFT VBA to duplicate everything. While it does leave with the first three letters, it is also unfortunately duplicating everything. In the example with the VBA below in leaves KUW in every row. Obviously I don't want this to happen. I know my code needs just a little tweak. Thank you,

VBA Code:
Sub UseLeft()
Dim r$: r = Range("H2", Range("H" & Rows.Count).End(xlUp)).Address
Range(r) = Evaluate("=Left(" & r & ", 3)")
End Sub

location.JPG
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
How about
VBA Code:
Sub UseLeft()
Dim r$: r = Range("H2", Range("H" & Rows.count).End(xlUp)).Address
Range(r) = Evaluate("if({1},Left(" & r & ", 3))")
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Just pointing out that since your data is so well structured (the three characters you want followed by a space-slash-space plus other text), you can use this simple one-liner to remove all but the first three characters...
VBA Code:
Sub UseLeft()
  Columns("H").Replace " / *", "", xlPart, , , , False, False
End Sub
 
Last edited:
Upvote 0
Just pointing out that since your data is so well structured (the three characters you want followed by a space-slash-space plus other text), you can use this simple one-liner to remove all but the first three characters...
VBA Code:
Sub UseLeft()
  Columns("H").Replace " / *", "", xlPart, , , , False, False
End Sub
Thank you so much for your input, reinforces there are multiple ways to get the same result. I did respond with more details regarding those dates you were helping me with the other day.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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