VBA - copying rows with data

timmetal

New Member
Joined
May 18, 2015
Messages
2
I currently have multiple monthly spreadsheets named "receipts Jan", "receipts Feb" etc. and currently writing a code which will open each spreadsheet to copy and paste data from row 19 only and then data from row 28 onward into one consolidated spreadsheet named "consolidated data". However, the data from row 28 may contain 1 row or 10 rows so I am unable to specify a particular range in my code, is there a way I can code it such it will only copy rows with values?

Secondly, as I will be pasting the data into the consolidated spreadsheet is it possible to code it such that the data from "receipt Feb" will be pasted directly one row below "receipt Jan"?

Hope I haven't confused anyone, thanks in advance!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello Timmetal,

Perhaps the following code may help:-


Code:
Sub Test()

Dim ws As Worksheet, sh As Worksheet, lr As Long, lr1 As Long, sr As Long

Set sh = Sheets("Consolidated Data")

Application.ScreenUpdating = False

For Each ws In Worksheets
       If ws.Name <> "Consolidated Data" Then
       lr = ws.Range("A" & Rows.Count).End(xlUp).Row
       ws.Range("A19").EntireRow.Copy sh.Range("A" & Rows.Count).End(3)(2)
       ws.Range("A28:A" & lr).EntireRow.Copy sh.Range("A" & Rows.Count).End(3)(2)
       sr = sh.Cells(sh.Rows.Count, "I").End(xlUp).Row + 1
       lr1 = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
       sh.Range("I" & sr & ":I" & lr1) = ws.Name
       End If
Next ws
       
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

As I don't know the extent of your data set, I've assumed the following:-

- Your data in each sheet starts in row2 with headings in row1.
- The Column range in all source sheets is A - H.
- The Column range in the Consolidated Data sheet is A - I with Column I being the column where the source sheet names are placed.

Secondly, as I will be pasting the data into the consolidated spreadsheet is it possible to code it such that the data from "receipt Feb" will be pasted directly one row below "receipt Jan"?

As long as your worksheets are in the correct order, this will happen with the code above.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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