Remove Characters From String

saltkev

Active Member
Joined
Oct 21, 2010
Messages
324
Office Version
  1. 2013
Platform
  1. Windows
I have a column of code numbers lets say A1 - A5000, each of these code numbers will vary in length, so I am looking to remove the last 12 carracters of each code number. I need to use VBA not Spread sheet formula.


Many Thanks in Advance



Saltkev
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What happens if a code number is 12 or fewer characters long? Assuming you want it left alone, try:
Code:
Sub RemoveLast12()

Dim i as Long

Application.ScreenUpdating = False

For i = 1 to Range("A" & Rows.Count).End(xlUp).Row
  If Len(Range("A" & i)) > 12 Then Range("A" & i) = Left(Range("A" & i), Len(Range("A" & i)) - 12)
Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad it helps

Slight improvement may be:
Rich (BB code):
Sub RemoveLast_j()
Dim i As Long
Const j As Long = 12
Application.ScreenUpdating = False
For i = 1 To range("A" & Rows.Count).End(xlUp).Row
  If Len(range("A" & i)) > j Then range("A" & i) = Left(range("A" & i), Len(range("A" & i)) - j)
Next i
Application.ScreenUpdating = True
End Sub
Where you can change the red 12 to whatever number you want to be the number of characters you remove from the right of each cell.

I've yet to get to grips with coding with arrays, but you could probably get some even faster code if arrays were used
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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