Need VBA to eliminate specific characters in cells

skyport

Active Member
Joined
Aug 3, 2014
Messages
374
Hoping someone can help with this one.


In columns A through G, I need a VBA code that will eliminate the first character only in each cell and only when that first character in the cell is a blank space or a colon ( : )or 0 (zero-numerical). However, all other cells would remain unchanged.


Please help if you can
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Code:
Sub REMOVESTUFF()

Dim tempstring As String
For Each cell In Sheets("sheet1").UsedRange
    
    tempstring = cell.Value
    If Left(cell, 1) = ":" Or Left(cell, 1) = " " Or Left(cell, 1) = 0 Then
        
    cell.Value = Right(tempstring, Len(tempstring) - 1)
    
    End If
    
Next cell


End Sub
 
Upvote 0
Another alternative.

Code:
Sub GetThoseNastyCharactersOuttaHere()
Dim c, r As Integer

For c = 1 To 7
    For r = 1 To 100    'I'm searching 100 rows, you can change this number to as many rows as you have data
        Select Case Left(Cells(r, c), 1)
            Case " ", ":", "0"
                Cells(r, c) = Right(Cells(r, c), Len(Cells(r, c)) - 1)
        End Select
    Next r
Next c

End Sub
 
Upvote 0
How are you calling this macro?

Is there a command button?

Do you want it do automatically happen whenever a cell in the worksheet is changed?
 
Upvote 0
hi tygrrboi. my comment above was in reference to the first idea that was posted. Your code may be working. I am running it now and testing. Will have feedback in 15 min. Thanks again for helping
 
Upvote 0
hi tygrrboi. got it to work and it is perfect thanks. Small detail that delayed me was a syntax involving a blank space after End Sub. When I removed it , all went well. Thanks again for the great solution.
 
Upvote 0
Here is another macro that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub RemoveLeadingSpaceColonOrZero()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF((LEFT(@)="" "")+(LEFT(@)="":"")+(LEFT(@)=""0""),MID(@,2,99),@)", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Here is another macro that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub RemoveLeadingSpaceColonOrZero()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF((LEFT(@)="" "")+(LEFT(@)="":"")+(LEFT(@)=""0""),MID(@,2,99),@)", "@", Addr))
End Sub[/td]
[/tr]
[/table]
While the above works (for me in my testing at least), so does this slightly more compact macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub RemoveLeadingSpaceColonOrZero()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(ISERROR(FIND(LEFT(@),"" :0"")),@,MID(@,2,99))", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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