Copying non-blank rows
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Copying non-blank rows

  1. #1
    Board Regular
    Join Date
    Nov 2016
    Posts
    364
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Copying non-blank rows

    Hi

    I'd like to copy all non-blank rows below a certain row.

    In the sample data below, the data starts in row 5 and goes up to column G.

    I'd like to copy a whole row IF the data in the first or second column of that row is populated.

    So if it was row 5, the row would only be copied IF there was a value in cell A5 or B5.....

    Also, the data is not contiguous, so there are some columns which are blank (column C and F in this example), but I would still like to copy the whole row if the cells in the first or second column of that row aren't blank.

    Has anyone done this before, please?

    A B C D E F G
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    2 2 2 2 2
    2 2 2 2 2
    2 2 2 2 2
    3 3 3 3 3
    3 3 3 3 3

  2. #2
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,692
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Copying non-blank rows

    You asked that the row be copied. But did not say where to copy it to.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  3. #3
    Board Regular
    Join Date
    Nov 2016
    Posts
    364
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copying non-blank rows

    Hi My Aswer Is This

    I'd want to copy the data to either a new sheet or new workbook, but I've managed to work this out.

    Thanks for your response, though.



  4. #4
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,692
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Copying non-blank rows

    I hope you figured it out on your own and did not get the results from another online help forum.
    Quote Originally Posted by Mr2017 View Post
    Hi My Aswer Is This

    I'd want to copy the data to either a new sheet or new workbook, but I've managed to work this out.

    Thanks for your response, though.


    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  5. #5
    Board Regular
    Join Date
    Nov 2016
    Posts
    364
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copying non-blank rows

    I worked it out myself.

  6. #6
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,692
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Copying non-blank rows

    Glad to hear that.

    Maybe you would like to show me the code you wrote.

    Quote Originally Posted by Mr2017 View Post
    I worked it out myself.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  7. #7
    Board Regular
    Join Date
    Nov 2016
    Posts
    364
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copying non-blank rows

    Hi My Aswer Is This

    Sure.

    In the real data that I have, I noticed that I could just use one column instead of two to decide whether I wanted to delete the blank rows.

    So using the sample data provided above, I deleted all blank rows in column B.

    I also had to copy and paste the formulas in column A as values, as when copying the data over to a new workbook, some of the formulas gave errors.

    I'd originally tried to loop through the data and copy all rows that were NOT blank using code I found on another site, but that took forever! Nearly 20mins, as there was lots of data!

    So deleting the blank rows based on a specific column is a MUCH QUICKER solution!

    Below is the code I used (modified to fit the sample data provided above).

    If you have any other questions about it, please let me know.

    Thanks


    Code:
    Sub DeleteBlankRows()
    
    
    'copy and paste the formulas in column A as values
    
    
    Range("A:A").EntireColumn.Copy
    Range("A:A").EntireColumn.PasteSpecial xlPasteValues
    
    
    'delete the blank rows in column B
    
    
    Range("B:B").Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    
    'copy the data
    
    
    Range("A:G").Copy
    
    
    End Sub

  8. #8
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,692
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Copying non-blank rows

    Thanks for sharing that.

    You can also do it this way:
    Code:
    Sub Convert_Column_Formulas_To_Values()
    'Modified 7/22/2019 6:40:39 AM  EDT
    Range("A:A").Value = Range("A:A").Value
    End Sub
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  9. #9
    Board Regular
    Join Date
    Nov 2016
    Posts
    364
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copying non-blank rows

    Ok, I didn't know that.

    Thanks.

  10. #10
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,692
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: Copying non-blank rows

    If you want to copy all the non blank rows by using column B to see if row is empty
    Try this script:

    It will copy the non blank rows to a sheet named "Summary" Row(4)
    This is using Filter which is very quick:

    Code:
    Sub Filter_Me_Please()
    'Modified  7/22/2019  7:10:14 AM  EDT
    Application.ScreenUpdating = False
    Dim Lastrow As Long
    Dim c As Long
    Dim s As Variant
    c = 2 ' Column Number Modify this to your need
    s = "<>" & "" 'Search Value Modify to your need
    Lastrow = Cells(Rows.Count, c).End(xlUp).Row
    Dim Counter As Long
    With ActiveSheet.Cells(1, c).Resize(Lastrow)
        .AutoFilter 1, s
        Counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
        If Counter > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Summary").Rows(4)
        Else
            MsgBox "No values found"
        End If
        .AutoFilter
    End With
    Application.ScreenUpdating = True
    End Sub
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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