none
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()
    'Updateby20131129
    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!

    SME


    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)
    Next
    End Sub
    
    Regards,
    Ashidacchi
    Saturday, October 22, 2016 6:09 AM