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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
=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
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,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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