Macro to remove leading spaces RRS feed

  • Question

  • Hi,

    I am not very experienced with VBA.  What I know, I've gathered from recording Macros and then examining them afterwards.  I'm trying to clean up data before I import into a database.  I ran this Macro once and it worked, but I had to manually exit because it seemed to loop and keep repeating itself.  Now it's throwing an error: Compile Error: For without Next.

    The top line is highlighted in the debugger.  

    Sub RemoveLeadingSpace()
    Dim Rng As Range
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each Rng In WorkRng
        Rng.Value = VBA.LTrim(Rng.Value)
    End Sub


    Many thanks for any clue to the problem!


    Friday, October 21, 2016 11:43 PM

All replies

  • Hi Sr.MiriamE,

    Synatx error:
        Missing "Next" for "For Each"...
        Modify like this   
    For Each Rng In WorkRng
        Rng.Value = VBA.LTrim(Rng.Value)
    End Sub
    Saturday, October 22, 2016 6:09 AM