Removing spaces from cells that have no visible content

xcellrodeo

Board Regular
Joined
Oct 27, 2008
Messages
206
Hi there, I have a set of data (example below) which runs from Col.B->AM. Number of rows varies.
What I would like to achieve is to have a VBA script that searches
through the columns starting from Col.E and ending in Col.AM to remove any spaces.
However this should only be done in cells that have no visible content so as to preserve any cells with content where the spaces are legitimate. The number of rows will depend on the size of the data which is variable.But for this, a count function could be added. Please note that empty columns between Col.E & AM can vary as below example only shows Col I & J as having no visible content.
Any help would be appreciated.Thanks
ABCDEFGHIJKL
2P/NCOUNTCOMPLETESUPPLCO1CO2CO3CO4CO5CO6CO7CO8
3K5506
ONE LINE90893R 200TECR-48245COROP-09 0223TRI9823 454

<tbody>
</tbody>
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
If the cells have only a single space you can use Replace, matching the entire contents (LookAt:=xlWhole).
 
Upvote 0
Hi Andrew, thanks for your suggestion.Unfortunately however there are no guarantees that cells with no visible value have only a single space. Some may have multiple.
 
Upvote 0
This may be a bit "brute force" but if you were able to put some limit on how many spaces there could be (I've used 10) then maybe something like this might be of use to you.

Rich (BB code):
Sub Clear_Space_Cells()
  Dim i As Long
  
  Const MaxSpaces As Long = 10
  
  Application.ScreenUpdating = False
  For i = 1 To MaxSpaces
    Columns("B:AM").Replace What:=String(i, " "), Replacement:="", LookAt:=xlWhole, SearchFormat:=False, ReplaceFormat:=False
  Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hey I'm pretty new at using VBA and this is probably a really inefficient way of doing it but I made a simple macro.

Code:
Sub RemoveSpaces()
Dim LastColumn As Integer
Dim LastRow As Integer
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Cells(Rows.Count, 1).End(xlUp).Row


For h = 1 To LastRow


    For i = 1 To LastColumn
    If Cells(h, i).Value = " " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "  " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "   " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "    " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "     " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "      " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "       " Then
      Cells(h, i).Value = Empty
    ElseIf Cells(h, i) = "        " Then
      Cells(h, i).Value = Empty
    End If
    Next
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,378
Members
449,097
Latest member
Jabe

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