VBA Code Fix: Appending All Rows to New Sheet, but One Row of Each is Missing

bklmabry

New Member
Joined
Dec 21, 2015
Messages
7
I was so proud when I modified the VBA code from JLGWhiz' reply to "VBA: Copy all rows with data from one sheet and paste on bottom of another" from this forum in order to copy/paste the rows from ten sheets, and it looked at first glance like it worked!

On closer inspection, I found that the final list of rows contained one less row per worksheet it copied from; i.e. worksheet 1 contains 13 rows but only 12 were copied. I'm sure there's something that needs to be tweaked in the code, but I'm only just learning VBA and can't quite root it out.

The original code from the earlier forum was:

Sub cpynpst()
Dim sh4 As Worksheet, sh5 As Worksheet, lr As long, rng As Range
Set sh4 = Sheets("Sheet4")
Set sh5 = Sheets("Sheet5")
lr= sh4.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh4.Range("A2:A" & lr)
rng.EntireRow.Copy sh5.Cells(Rows.Count, 1).End(xlUp)(2)
End Sub

It's not the most elegant, I'm sure, but I modified it thus:

Sub cpynpst()

Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet, sh5 As Worksheet
Dim sh6 As Worksheet, sh7 As Worksheet, sh8 As Worksheet, sh9 As Worksheet, sh10 As Worksheet
Dim sh0 As Worksheet, lr As Long, rng As Range

Set sh1 = Sheets("1")
Set sh2 = Sheets("2")
Set sh3 = Sheets("3")
Set sh4 = Sheets("4")
Set sh5 = Sheets("5")
Set sh6 = Sheets("6")
Set sh7 = Sheets("7")
Set sh8 = Sheets("8")
Set sh9 = Sheets("9")
Set sh10 = Sheets("10")
Set sh0 = Sheets("Sum")

lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh2.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh3.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh3.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh4.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh4.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh5.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh5.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh6.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh6.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh7.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh7.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh8.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh8.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh9.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh9.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

lr = sh10.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh10.Range("A2:A" & lr)
rng.EntireRow.Copy sh0.Cells(Rows.Count, 1).End(xlUp)(2)

End Sub



I'm almost certain that something in those last paragraphs is causing the module to copy one less row per worksheet or copy over prior rows in each worksheet. Can someone, maybe JLGWhiz, offer some help?

Thank you!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
JackDanIce! It worked! The code with the worksheets listed actually worked! It's just the the module aborted because one of the sheets had no data to append to the final list! I'm going to try the other code as well because in that one, it looks like it doesn't require the itemized worksheet names. Thanks so much! I'll report back after I've tried it.

Thanks,
 
Upvote 0
Glad you resolved it, I just tried a dummy version of your file and it worked fine for me so couldn't tell what was going wrong.
 
Upvote 0
Last edited:
Upvote 0

Forum statistics

Threads
1,216,088
Messages
6,128,744
Members
449,466
Latest member
Peter Juhnke

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