Looping through worksheets fails

Network19

New Member
Joined
Nov 21, 2017
Messages
4
Hi there,

I have been coding for a while so know the basics fairly well. I have, however come across an issue (Code is posted below).

I am trying to write a macro where it will create a new sheet, name it and do some basic stuff but then i want it to loop through any other worksheets available in the workbook and copy the used range there and paste it in the newly created sheet in order.

Theoretically, i would ask it to offset but i have been unsuccessful. I would appreciate any help with the code and perhaps guidance as to where I've gone wrong.

Many thanks


Code:

Sub FeeStatement()
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Consolidated Fee Statement"
Worksheets("Consolidated Fee Statement").Range("A1").Value = "Insert Name"
Worksheets("Consolidated Fee Statement").Range("A1").Font.Bold = True
Worksheets("Consolidated Fee Statement").Range("A2").Value = "Fee Statement"
Worksheets("Consolidated Fee Statement").Range("A3").Value = Date
Worksheets("Consolidated Fee Statement").Range("A3").Select
Selection.Copy
Worksheets("Consolidated Fee Statement").Range("A3").PasteSpecial xlPasteValues
Dim i As Worksheet
For Each i In ActiveWorkbook.Sheets
If ws.Name <> "Consolidated Fee Statement" Then
ws.Activate
i.UsedRange("A1:M5").Select
Selection.Copy
Worksheets("Consolidated Fee Statement").Range("A10" & Count).PasteSpecial xlPasteValues
Else: MsgBox "Error"
End If
Next i
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, welcome to the board.
If you only want to copy A1:M5 to the new sheet try
Code:
Sub FeeStatement()

    Dim Ws As Worksheet
    
Application.ScreenUpdating = False
    Sheets(1).Select
    Worksheets.Add
    Sheets(1).Name = "Consolidated Fee Statement"
    With Sheets(1)
        .Range("A1").Value = "Insert Name"
        .Range("A1").Font.Bold = True
        .Range("A2").Value = "Fee Statement"
        .Range("A3").Value = Date
        For Each Ws In Worksheets
            If Ws.Name <> "Consolidated Fee Statement" Then
                Ws.Range("A1:M5").copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        Next Ws
    End With
End Sub
 
Upvote 0
Hi, many thanks for your response. I put A1:M5 because used range didnt work. I essentially want it to copy whatever range used in each sheet and paste it in the master sheet
 
Upvote 0
ok, try this
Code:
Sub FeeStatement()

    Dim Ws As Worksheet
    Dim ShtExist As Boolean
    
Application.ScreenUpdating = False

    On Error Resume Next
    ShtExist = (Sheets("Consolidated Fee Statement").Name) = ("Consolidated Fee Statement")
    On Error GoTo 0
    If ShtExist Then
        MsgBox "Sheet ""Consolidated Fee Statement"" already exists"
        Exit Sub
    End If
    Worksheets.Add before:=Sheets(1)
    Sheets(1).Name = "Consolidated Fee Statement"
    With Sheets(1)
        .Range("A1").Value = "Insert Name"
        .Range("A1").Font.Bold = True
        .Range("A2").Value = "Fee Statement"
        .Range("A3").Value = Date
        For Each Ws In Worksheets
            If Ws.Name <> "Consolidated Fee Statement" Then
                Ws.UsedRange.copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        Next Ws
    End With
End Sub
I've also put in a check to see if the new sheet already exists
 
Upvote 0
ok, try this
Code:
Sub FeeStatement()

    Dim Ws As Worksheet
    Dim ShtExist As Boolean
    
Application.ScreenUpdating = False

    On Error Resume Next
    ShtExist = (Sheets("Consolidated Fee Statement").Name) = ("Consolidated Fee Statement")
    On Error GoTo 0
    If ShtExist Then
        MsgBox "Sheet ""Consolidated Fee Statement"" already exists"
        Exit Sub
    End If
    Worksheets.Add before:=Sheets(1)
    Sheets(1).Name = "Consolidated Fee Statement"
    With Sheets(1)
        .Range("A1").Value = "Insert Name"
        .Range("A1").Font.Bold = True
        .Range("A2").Value = "Fee Statement"
        .Range("A3").Value = Date
        For Each Ws In Worksheets
            If Ws.Name <> "Consolidated Fee Statement" Then
                Ws.UsedRange.copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
        Next Ws
    End With
End Sub
I've also put in a check to see if the new sheet already exists

Thank you so much! I really appreciate this. I have been stuck on this for a whole day! You are a legend :)
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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