Loop Fail: Only running code on Last worksheet

Amco88

New Member
Joined
Jan 25, 2018
Messages
3
Hi,

I have a project where I need to copy select cell values from 200+ sheets into One sheet (Kind of like a report).

The cell numbers are the same across all worksheets,

I managed to get the copy portion to work but its only running it on one worksheet, for some reason its the last sheet.

Im completely new to VBA so bare with me. Below is what I am running:




Code:
Sub Macro8()
  Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim LastRow As Long
    Dim CopyRng As Range


    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    'Delete the sheet "Report" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("Report").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True


    'Add a worksheet with the name "Report"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "Report"


    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> DestSh.Name Then
            'selecting the next row in report
        
            'Find the last row with data on the DestSh
            LastRow = DestSh.Range("A1").SpecialCells(xlCellTypeLastCell).Rows
           
            'Fill in the range that you want to copy
            Set CopyRng = sh.Range("A2:C2, BI2:BK2")


            'This example copies values/formats, if you only want to copy the
            'values or want to copy everything look at the example below this macro
            CopyRng.Copy
            With DestSh.Cells(Last + 1, "A")
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            End With


            End If
    Next sh


End Sub
 
Last edited by a moderator:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
To find your last row you need .Row not .Rows then you actually need to use LastRow not Last as you have used.
 
Upvote 0
To find your last row you need .Row not .Rows then you actually need to use LastRow not Last as you have used.

Thanks for the quick response. Made the changes as stated but still only copying the cells from the last sheet as opposed to all sheets:

Sub Macro8()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim LastRow As Long
Dim CopyRng As Range


With Application
.ScreenUpdating = False
.EnableEvents = False
End With


'Delete the sheet "Report" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Report").Delete
On Error GoTo 0
Application.DisplayAlerts = True


'Add a worksheet with the name "Report"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Report"


'loop through all worksheets and copy the data to the DestSh
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name Then




'Find the last row with data on the DestSh
LastRow = DestSh.Range("A1").SpecialCells(xlCellTypeLastCell).Row
'Fill in the range that you want to copy
Set CopyRng = sh.Range("A2:C2, BI2:BK2")



'values or want to copy everything look at the example below this macro
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With


End If
Next sh


End Sub
 
Upvote 0
You still havent changed Last to LastRow. Type option explicit right at the top of your module. It will point it out to you.
 
Upvote 0
It will be looping but at each loop it pastes the data over the previously pasted data which is why you are only seeing the last sheets data.
 
Upvote 0
It will be looping but at each loop it pastes the data over the previously pasted data which is why you are only seeing the last sheets data.
Beautiful!! Totally worked that time! Thanks for your help. I've been working with this for hours, you are a life saver :)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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