removing excessive spaces

Craig68

New Member
Joined
Mar 6, 2025
Messages
3
Office Version
  1. 365
  2. 2024
  3. 2021
Platform
  1. Windows
Hi,
I have the following code to remove excess spaces from the selected range (this could even be a single cell).
The problem is I get a run-time error if the selection is empty.
What I would like is to check the selection and if all cells are empty then pop-up a message "Selection is empty"
If not then carry on.

VBA Code:
Sub Remove_Spaces()
    Dim r As Range
    With Application.WorksheetFunction
    For Each r In Intersect(Selection, ActiveSheet.UsedRange)
        r.Value = .Trim(r.Value)
    Next r
    End With
End Sub
 
If the selected cells are empty, it should not cause a run-time error. If all the selected cells are outside the used range you will get a Run-Time error '424' Object required.
What error message are you getting and what line of code is highlighted when you click on debug ?

The below would avoid the 424 error:

VBA Code:
Sub Remove_Spaces()
    Dim r As Range
    Dim rng As Range
    
    Set rng = Intersect(Selection, ActiveSheet.UsedRange)
    
    'If rng Is Nothing Then Exit Sub
    With Application.WorksheetFunction
        For Each r In rng
            r.Value = .Trim(r.Value)
        Next r
    End With
End Sub
 
Upvote 0
like this ?

VBA Code:
Sub Remove_Spaces()
    'On Error Resume Next
    
    Dim r As Range
    With Application.WorksheetFunction
    
            If .CountA(Selection) = 0 Then
                MsgBox "Cell Is Empty"
                Exit Sub
            End If
            
    For Each r In Selection
            r.Value = .Trim(r.Value)
    Next r
    End With
End Sub
 
Upvote 0
Solution
Thanks for the reply, this is the error message when the selected range is empty.

1741315472284.png


I have tried your code and still get the same error.
This line is highlighted when using your code "For Each r In rng"
This line was highlighted in my original code "For Each r In Intersect(Selection, ActiveSheet.UsedRange)"

VBA Code:
Sub Remove_Spaces()
    Dim r As Range
    Dim rng As Range
    
    Set rng = Intersect(Selection, ActiveSheet.UsedRange)
    
    'If rng Is Nothing Then Exit Sub
    With Application.WorksheetFunction
        For Each r In rng
            r.Value = .Trim(r.Value)
        Next r
    End With
End Sub
 
Upvote 0
Thankyou this works perfectly


like this ?

VBA Code:
Sub Remove_Spaces()
    'On Error Resume Next
   
    Dim r As Range
    With Application.WorksheetFunction
   
            If .CountA(Selection) = 0 Then
                MsgBox "Cell Is Empty"
                Exit Sub
            End If
           
    For Each r In Selection
            r.Value = .Trim(r.Value)
    Next r
    End With
End Sub
 
Upvote 0
Thankyou this works perfectly
Just and FYI - Sunny's code is not working because of the "empty" test but because it has removed using the Intersect with the used range requirement.
It just uses whatever cells were selected.
If you know for a fact that you are going to only ever run it on a contiguous set of cells you don't even need to loop.
 
Upvote 0

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