Consulting

Results 1 to 4 of 4

Thread: Solved: Delete any spaces in cell

  1. #1
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location

    Solved: Delete any spaces in cell

    Hi

    does anyone know of a quick macro that will check each cell for blank spaces and delete them?

    Thanks for any help
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Try this - select a range and run
    [vba]
    Public Function RemoveSpaces(strInput As String)
    ' Removes all spaces from a string of text
    ' This can also remove any character from a string
    ' Replace the " " with whatever character you want
    ' IE - "a" will remove all of the a's from a string
    test:
    If InStr(strInput, " ") = 0 Then
    RemoveSpaces = strInput
    Else
    strInput = Left(strInput, InStr(strInput, " ") - 1) _
    & Right(strInput, Len(strInput) - InStr(strInput, " "))
    GoTo test
    End If
    End Function


    Sub DeleteSpaces()
    Application.ScreenUpdating = False
    On Error Resume Next
    Dim objCell As Object
    For Each objCell In Selection.Cells
    objCell.Value = RemoveSpaces(objCell.Value)
    Next objCell
    Application.ScreenUpdating = True
    End Sub
    [/vba]

  3. #3
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Fantastic JKwan - works a treat.

    Thanks for your help!
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To LastRow

    .Cells(i, TEST_COLUMN).Value = Replace(.Cells(i, TEST_COLUMN).Value, " ", "")
    Next i

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •