Results 1 to 13 of 13

Thread: Align Row Content with cell format to Left column & Delete Blank row

  1. #1

    Align Row Content with cell format to Left column & Delete Blank row

    Hi, there
    I have 100Rows plus in 1 worksheet. Would you please help to state macro to Align Row Content to Left Column (including Cell format & color) and delete the blank row as well?

    P.S. Enclosed the Excel file for your reviewing. The worksheet - sheet1 is the original data & the Result tab is what I want.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by JOEYSCLEE; 11-16-2016 at 06:31 AM.

  2. #2
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Quick and crude. This code goes in the applicable Worksheet Code Page. If you need it for more than one sheet, see below.
    Option Explicit
    
    Sub VBAX_AlignToLeft()
    Dim LastRow As Long
    Dim i As Long
    
    LastRow = InputBox("What is the last Row number to align?", "Select Row Number")
    
    With Range("A:A")
      For i = 1 To LastRow
    If WorksheetFunction.CountA(Rows(i)) > 0 Then
        Do While .Cells(i).Value = ""
          .Cells(i).Delete xlShiftToRight
        Loop
    End If
      Next
    
    End With
    
    
    End Sub
    This code is useable on any sheet. It goes in a Standard Module
    Option Explicit
    
    Sub VBAX_AlignToLeft()
    Dim LastRow As Long
    Dim i As Long
    
    LastRow = InputBox("What is the last Row number to align?", "Select Row Number")
    
    With ActiveSheet
    
    With Range("A:A")
      For i = 1 To LastRow
    If WorksheetFunction.CountA(Rows(i)) > 0 Then
        Do While .Cells(i).Value = ""
          .Cells(i).Delete xlShiftToRight
        Loop
    End If
      Next
    
    End With
    End With
    
    
    End Sub
    Please take the time to read the Forum FAQ

  3. #3
    Thanks for the reply! After running both codes, they don't work and showed Run-time error'1004 & Delete method of Range class failed. Meanwhile, .Cells(i).Delete xlShiftToRight is highlighted after debug
    Error 1004.jpg

  4. #4
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    My bad

    try xlShiftToLeft

    Shift right is not an option when deleting cells. It's either shift up or shift left.
    Please take the time to read the Forum FAQ

  5. #5
    Thank you for the quick response!!

    The following Macro is working for Aligning Row Content To Left. However, could you please to add the line to delete BLANK Row with the below code?


    Option Explicit

    Sub VBAX_AlignToLeft()
    Dim LastRow As Long
    Dim i As Long

    LastRow = InputBox("What is the last Row number to align?", "Select Row Number")


    With Range("A:A")
    For i = 1 To LastRow
    If WorksheetFunction.CountA(Rows(i)) > 0 Then
    Do While .Cells(i).Value = ""
    .Cells(i).Delete (xlShiftToLeft)
    Loop
    End If
    Next

    End With

    End Sub

  6. #6
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Remember, this version must be in the applicable Worksheet's Code page. You can make the same change to the other version.

    Option Explicit
     
    Sub VBAX_AlignToLeft()
        Dim LastRow As Long
        Dim i As Long
             
        LastRow = InputBox("What is the last Row number to align?", "Last Row Number")
         
     
            With Range("A:A")
                For i = LastRow to i Step -1
                    If WorksheetFunction.CountA(Rows(i)) > 0 Then 
                        Do While .Cells(i).Value = ""
                            .Cells(i).Delete (xlShiftToLeft)
                        Loop
                     Else
                        Rows(i). Delete
                    End If
                Next             
            End With    
         
    End Sub
    When Deleting Rows, always work from the bottom up
    Please take the time to read the Forum FAQ

  7. #7
    Thank you for the comment!!
    The Revised Code is workable But it showed Run-time error'1004' after running the macro. The following line is "If WorksheetFunction.CountA(Rows(i)) > 0 Then" is highlighted.

    Debug Error.JPG

  8. #8
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    What does Run Time Error 1004 say?

    Please use the Go Advanced page to upload some sample data.
    Please take the time to read the Forum FAQ

  9. #9

    Run Time Error 1004

    The Error messaged is Application-defined or object-defined error

    Run time error 1004.jpg

  10. #10
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    In the highlighted line, change "Rows" to "ActiveSheet.Rows"
    Please take the time to read the Forum FAQ

  11. #11
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,970
    For i = LastRow To i Step -1
    ?!
    For i = LastRow To 1 Step -1
    surely.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  12. #12
    Thanks p45cal!! There is no more error code after running the macro.

    Also, Thanks SamT...You helped me a lot with your advised code.

  13. #13
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Surely, 1ndeed.

    That's a real bad habit my keyboard has. I want "i" and I get "1". I want "1" and I get "!". Sometimes when I want "i", I even get "!". But never the reverse.

    What can I say Dude, it's a Dell.
    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

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