Link to home
Start Free TrialLog in
Avatar of kbay808
kbay808Flag for United States of America

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.
Avatar of ReneD100
ReneD100
Flag of Netherlands image

Clarify 'remove spaces' please. Should 'This is a test' become 'Thisisatest'? Or leading spaces? Or fields contain no data but spaces?
Avatar of kbay808

ASKER

Before
   This is a test   1

After
Thisisatest1
ASKER CERTIFIED SOLUTION
Avatar of ReneD100
ReneD100
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbay808

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

Open in new window

I see you already asked this question somewhere else and got an answer. So you're all set.
Avatar of kbay808

ASKER

Yes, thank you
Avatar of kbay808

ASKER

Thanks!