Help with simple row copying macro

slyman007

New Member
Joined
Aug 10, 2013
Messages
2
Hi, all:

I'm using a macro to check if a column contains data, and if so, copy its row to another worksheet. It's working great as long as there's data in the column I'm searching. If there's no data in that column, instead of doing nothing, it copies the header row over. How can I prevent it from copying the header row if there are no results in that column? I'm actually not sure why it's doing that in the first place, as the range I specified leaves out row 1, which is the header row. And, it doesn't copy the header row if there are any other rows that have data in that column.

Thanks in advance for any guidance!

Code:
Sub CopyStuff()

    Dim bottomL3 As Integer
    Dim x3 As Integer
    bottomL3 = Sheets("Data").Range("U" & Rows.Count).End(xlUp).Row: x3 = 1
     
    Dim c3 As Range
    For Each c3 In Sheets("Data").Range("U2:U" & bottomL3)
        If c3.Value <> "" Then
            c3.EntireRow.Copy Worksheets("Data3").Range("A" & x3)
            x3 = x3 + 1
        End If
    Next c3

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hello skyman007,

You need to exit if "bottomL3" is less than the row of the starting cell which is "U2".
Code:
Sub CopyStuff()

    Dim bottomL3    As Integer
    Dim c3          As Range
    Dim x3          As Integer
    
        bottomL3 = Sheets("Data").Range("U" & Rows.Count).End(xlUp).Row: x3 = 1
        If bottomL3 < 2 Then Exit Sub
        
        For Each c3 In Sheets("Data").Range("U2:U" & bottomL3)
            If c3.Value <> "" Then
                c3.EntireRow.Copy Worksheets("Data3").Range("A" & x3)
                x3 = x3 + 1
            End If
        Next c3

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top