kbay808
asked on
How to create a vba code to run on the sheet to automatically remove all spaces in cells B3, B4 and B12?
I need a macro to automatically run on sheet1 to automatically remove all spaces in cells B3, B4 and B12.
Clarify 'remove spaces' please. Should 'This is a test' become 'Thisisatest'? Or leading spaces? Or fields contain no data but spaces?
ASKER
Before
This is a test 1
After
Thisisatest1
This is a test 1
After
Thisisatest1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works great, but it won't work unless I delete the existing macro already in that sheet. How do I combine the two codes into one?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changed As Range, c As Range
Dim cVal
Const myR As String = "B5,B9" '<- Your range(s)
Set changed = Intersect(Target, Range(myR))
If Not changed Is Nothing Then
Application.EnableEvents = False
For Each c In changed
cVal = c.Value
Select Case True
Case IsEmpty(cVal), IsNumeric(cVal), _
IsDate(cVal), IsError(cVal)
' Do nothing
Case Else
c.Value = UCase(cVal)
End Select
Next c
Application.EnableEvents = True
End If
End Sub
I see you already asked this question somewhere else and got an answer. So you're all set.
ASKER
Yes, thank you
ASKER
Thanks!