+ Reply to Thread
Results 1 to 13 of 13

Macro that removes blank rows?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Macro that removes blank rows?

    I have a log that is A:N and varies on how many rows. I have a macro (below) that combines my several logs into one mother log. Problem is, if one of my reps skips a line then this macro will not grab the stuff below that. Is there a way to remove those accidental skips, or would it be better for my macro to be altered to accommodate for those? Even though it is just a blank sheet with a few headers, I have attached what we use.



    Sub DCMRCombine()
    Dim J As Long, k As Long, r As Range
    J = Worksheets.Count
    With Worksheets("DCMR")
    Set r = Range(.Range("A2"), .Range("A2").End(xlDown))
    r.EntireRow.Delete
    End With
    
    For k = 1 To J
    If Worksheets(k).Name = "DCMR" Then GoTo errorhandler
    With Worksheets(k)
    If .Range("A2") = "" Then GoTo errorhandler
    Set r = Range(.Range("A2"), .Range("A2").End(xlDown))
    r.EntireRow.Copy
    Worksheets("DCMR").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
    End With
    errorhandler:
    Next k
    ' Center Macro
    '
    
    '
        Cells.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,935

    Re: Macro that removes blank rows?

    Below portion of your code after "With Worksheets(k)" line, is what stops range at next empty row.
    Set r = Range(.Range("A2"), .Range("A2").End(xlDown))
    Change to...
    Set r = .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Rows)

  3. #3
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro that removes blank rows?

    CK76

    When I do this, I get a 1004 error. Application defined or object defined error

    Sub DCMRCombine()
    Dim J As Long, k As Long, r As Range
    J = Worksheets.Count
    With Worksheets("DCMR")
    Set r = Range(.Range("A2"), .Range("A2").End(xlDown))
    r.EntireRow.Delete
    End With
    
    For k = 1 To J
    If Worksheets(k).Name = "DCMR" Then GoTo errorhandler
    With Worksheets(k)
    If .Range("A2") = "" Then GoTo errorhandler
    Set r = .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Rows)
    r.EntireRow.Copy
    Worksheets("DCMR").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
    End With
    errorhandler:
    Next k
    ' Center Macro
    '
    
    '
        Cells.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    End Sub

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,017

    Re: Macro that removes blank rows?

    Run this macro to delete any blank rows

    Sub delBlank()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
    If IsNull(Range("A" & i)) Then Range("A" & i).EntireRow.Delete
    Next i
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro that removes blank rows?

    alansidman

    This didn't seem to work. I filled in about 30 rows with data, then selected about 5 random rows and delete the data out. Ran this macro and nothing happened.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Macro that removes blank rows?

    Instead of xldown, count the rows.

    Sub Button2_Click()
        Dim r As Range, sh As Worksheet, LstRw As Long
        Set sh = Sheets("Sheet1")
        With sh
            LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
            Set r = .Range("A2:A" & LstRw)
        End With
        r.Select
    End Sub

  7. #7
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro that removes blank rows?

    davesexcel

    Similar thing as Alan's. I filled in about 30 rows with data, then selected about 5 random rows and delete the data out. Ran this macro and nothing happened.

    this was on a new blank workbook.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,935

    Re: Macro that removes blank rows?

    I had no issue running the code. At any rate I'd suggest going with how davesexcel showed you and defining last row in separate line.

    Sub DCMRCombine()
    Dim J As Long, k As Long, r As Range
    J = Worksheets.Count
    With Worksheets("DCMR")
    lRow = .Cells(Rows.Count, "A").End(xlUp).Row
    Set r = .Range("A2:A" & lRow)
    r.EntireRow.Delete
    End With
    
    For k = 1 To J
    If Worksheets(k).Name = "DCMR" Then GoTo errorhandler
    With Worksheets(k)
    If .Range("A2") = "" Then GoTo errorhandler
    lRow = .Cells(Rows.Count, "A").End(xlUp).Row
    Set r = .Range("A2:A" & lRow)
    r.EntireRow.Copy
    Worksheets("DCMR").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
    End With
    errorhandler:
    Next k
    ' Center Macro
    '
    
    '
        Cells.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        
    End Sub

  9. #9
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro that removes blank rows?

    But I can't define the last row, the last row may be row 75 on one sheet, or row 50 on another sheet.

  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Macro that removes blank rows?

    Not sure what you mean, the code counts the rows. It finds the very last filled row in column A

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Macro that removes blank rows?

    How about this ?

    Sub DCMRCombine()
        Dim k As Long, ws As Worksheet, lRow As Long
        Set ws = Sheets("DCMR")
        With ws
            lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            If lRow <> 1 then .Rows("2:" & lRow).Clear
        End With
    
        For k = 1 To Sheets.Count
            If Sheets(k).Name = "DCMR" Then GoTo errorhandler
            With Sheets(k)
                If .Range("A2") = "" Then GoTo errorhandler
                lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                .Range("A2:N" & lRow).Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
            End With
    errorhandler:
        Next k
    ' Center Macro
    '
    
    '
        Cells.Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        
    End Sub
    The reason why is quite simple.
    At the start of your code you clear the contents of sheet DCMR. If you then do a Rows.Count without specifying on which sheet and DCMR is the active one when running the code you always get 1 for your LastRow count.
    Therefor nothing happens on your other sheets.
    Last edited by bakerman2; 02-17-2017 at 09:33 PM. Reason: Added explanation.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Macro that removes blank rows?

    Thanks bakerman, that works well, only issue is that it also copies the blank row over. So then when I run a pivot table off the data, the table wants to stop at the blank row.

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Macro that removes blank rows?

    Alan posted code in Post#3 to delete blank rows. Use that after the copying.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro that removes empty rows within a table
    By airedale360 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-10-2016, 07:24 PM
  2. How to create a macro to insert blank rows and copy data into blank rows?
    By zodiack101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2013, 01:18 PM
  3. Macro to delete blank rows if column I is blank for the row
    By tiger10012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-10-2013, 03:01 PM
  4. Replies: 5
    Last Post: 05-09-2013, 01:16 AM
  5. [SOLVED] Macro that removes rows based on frequency and copies part of the row to a new sheet
    By njmiller31 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-28-2012, 05:32 PM
  6. Applying autofilter removes rows from sight outside of table
    By mcneill_garr in forum Excel General
    Replies: 5
    Last Post: 08-15-2011, 02:32 PM
  7. VBA that removes duplicate rows
    By Blindbert in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-21-2007, 12:32 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1