VBA delete characters from cell

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I am looking for a macro which deletes the first 3 characters of a text string in a cell but only if the first character begins with the letter "A".

could someone help?

thanks
andy
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Sub test()
With Sheets("Sheet1").Range("A1")
If Left(.Value, 1) = "A" Then .Value = Right(.Value, Len(.Value) - 3)
End With

End Sub
 
Upvote 0

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
=IF(LEFT(A1,1)="A",REPLACE(A1,1,3,""),A1)
translated to VBA
Code:
If Left(Range("A1"),1) = "A" Then 
    Range("A1").Value = WorksheetFunction.Replace(Range("A1").Value, 1, 3, "")
End if
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hey Neil,

How would I do this, but rather than the first character being an 'A', it's any number?
Using Neil's code layout and assuming you mean you still want to delete the first 3 characters for that condition, change the If..Then code line to this...
Code:
Sub test()
  With Sheets("Sheet1").Range("A1")
    If .Value Like "#*" Then .Value = Mid(.Value, 4)
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,029
Messages
5,984,226
Members
439,878
Latest member
melodysc

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
Top