Macro to copy rows with data from one sheet to another...

LittleAngel

New Member
Joined
Jul 30, 2002
Messages
12
I have 2 worksheets. I'd like to have a macro (I am new to macros) that does the following:

- Starts in Sheet 1 in a cell that you pick (usually row 7 column A)
- Checks that row if it's blank
- If it's not blank, copy the row to sheet 2 (row 1)
- if it is blank, check the next row for data.
- If the next row has data, copy that data after the last copied data in sheet 2 (row 2)
- if it's blank, quit.
- Keep checking for blank rows and copying into sheet two until it comes to two consecutive rows that are blank.

Thank you!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

The following worked for me. Please try this and report your results.

Code:
Option Explicit

Sub test()
Dim wks1 As Worksheet, wks2 As Worksheet
Dim LastRow As Long, StartRow As Long, x As Long
Dim rng1 As Range, rng2 As Range, FinishRow As Long
Dim fn As WorksheetFunction

Set fn = Application.WorksheetFunction
Set wks1 = ThisWorkbook.Sheets("Sheet1")
Set wks2 = ThisWorkbook.Sheets("Sheet2")

wks2.Cells.ClearContents

With wks1
    On Error Resume Next
    StartRow = _
    Application.InputBox(Prompt:="Select any cell in your beginning row", Type:=8).Row
    If Not IsNumeric(StartRow) Then Exit Sub
    Err.Clear
    On Error GoTo 0
    
    LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
    FinishRow = LastRow
    For x = StartRow To LastRow
        If fn.CountA(.Rows(x)) = 0 And fn.CountA(.Rows(x + 1)) = 0 Then
            FinishRow = fn.Max(StartRow, x - 1)
            Exit For
        End If
    Next x
    Set rng1 = .Range(.Cells(StartRow, 1), .Cells(FinishRow, 256))
    rng1.Copy wks2.Range("A1")
End With

With wks2
    LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
    For x = LastRow To 1 Step -1
        If fn.CountA(.Rows(x)) = 0 Then .Rows(x).Delete shift:=xlUp
    Next x
End With

End Sub
 
Upvote 0
What if instead of the row being "not blank" that a cell in the row met certain criteria like "Starts with a 1"?

Could I just alter the above macro to pull from sheet 1 an entire row where column b "starts with an L" or "an M" and paste those rows in sheet 2?

Thanks.
 
Upvote 0
For instance if column B starts with an L could I set it up to automatically pull those entire rows into a summary sheet in order separate jobs by salesperson and tally totals.
job cost template2.xls
ABCDEFGHIJKLMNO
4NameJob NumberJob TypeTotal ContractRbA Cost%Material Cost%Labor Cost%Outside%Production OverheadGross Profit%
5McKinleyL56553Win19,9727,276361,79496503024010,01250
6JacobsL56874Win2,635677265812201104601,20746
7MayerM52234Door8810148173363803036742
8MayerM52234Siding27,10006,8502512,350465,4202002,4809
9ConleyM53514Door2,4400264111,5306303061625
10LokkenM58742Carp53,500025,2004716,400312,430509,47018
11DoranS50912Roof11,20002,220203,5403205,44049
12KuntzS51015Win12,6653,250264574002208,73869
List
 
Upvote 0
sorry i have a few questions regarding the code..

how do i:

- edit the StartRow to hardcode it to the Row & Column pre-defined
- edit it to allow me to choose which cell to check
- edit it to allow me to choose which cell to copy

pls help me..!
 
Upvote 0
I need to do something similar, so I used the code as written. Unfortunately, I get the error message "Subscript out of range" on this line.

Set wks2 = ThisWorkbook.Sheets("Sheet2")

I copied and pasted into a new module and ran it from there, so there are no typos

Can anyone shed any light on the problem?

David
 
Upvote 0
I need to do something similar, so I used the code as written. Unfortunately, I get the error message "Subscript out of range" on this line.

Set wks2 = ThisWorkbook.Sheets("Sheet2")

I copied and pasted into a new module and ran it from there, so there are no typos

Can anyone shed any light on the problem?

David

The workbook that contains the code must have a sheet named "Sheet2" or else the code will throw the error you received.
 
Upvote 0
Hi,

Is it possible to do the same thing, except rather than retrieving any row that contains data, retrieving only those rows where column A contains a date between 1996 and 1998? And then, rather than retrieving the entire row, only retrieving the data from columns A and H?

Thanks.
 
Upvote 0
how was your date in column inputted? is it in date format or plane 1996,1997, etc? are you going to retrieve only those dates and corresponding row data? for example, you want to retrieve date between 1996 and 1998, the sheets2 will show only these dates and row data?
 
Upvote 0

Forum statistics

Threads
1,215,340
Messages
6,124,386
Members
449,155
Latest member
ravioli44

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