Help looping a macro please

wallyxl

Board Regular
Joined
Apr 24, 2011
Messages
100
Hello everyone,
I have recorded a macro and it works fine.
I have 18 sheets that are numbered 1 through to 18.
I would like this macro to loop through each sheet from 1 to 18 as I will need to extract data from each sheet about 4 times (I can change the range(D25) each time I run it.

Thank you.

Code:
Sheets("18").Select
    Range("D25").Select
    Selection.Copy
    ActiveCell.Offset(65, 0).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(-65, 2).Range("A1").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveCell.Offset(65, -3).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(-62, 2).Range("A1:D7").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveCell.Offset(62, 0).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(-62, 23).Range("A1:B7").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveCell.Offset(62, -19).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, -6).Range("A1:B1").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveCell.Offset(1, 0).Range("A1:A6").Select
    ActiveSheet.Paste
    ActiveCell.Rows("1:1").EntireRow.Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
    Selection.Delete Shift:=xlUp
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
    Selection.Delete Shift:=xlUp
    ActiveCell.Offset(-3, 2).Range("A1:H4").Select
    Selection.Cut
    Sheets("Catchall").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Select
    Selection.End(xlUp).Select
End Sub
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Your Macro can cleaned up to increase speed and shorten it up a bit (it is usually not necessary to select ranges to work with them in VBA, and selecting them actually slows down your code).
We can help you clean it up, but rather than trying to reverse engineer your whole code, can you just explain the steps it should be doing in plain English?
 
Upvote 0
As I just finished cleaning up the code.
This should do what your macro does & will loop through the sheets
Code:
Sub CopyPaste()
   Dim Ws As Worksheets
   
   For Each Ws In Sheets([COLOR=#0000ff]Array("1", "2", "3", "4")[/COLOR])
      With Ws
         .Range("D25").Copy .Range("D90")
         .Range("F25").Copy .Range("C90")
         .Range("E28:H34").Copy .Range("E90")
         .Range("AB28:AC34").Copy .Range("I90")
         .Range("C90:D90").Copy .Range("C91:C96")
         .Range("91:91,93:93,95:95").EntireRow.Delete
         .Range("C90:J93").Cut Sheets("Catchall").Range("A" & Rows.Count).End(xlUp).Offset(1)
      End With
   Next Ws
End Sub
Just add the rest of the sheets to the array in blue, in the same manner
 
Upvote 0
Hi Joe4 and Fluff,
Fluff your code looks spot on (it looks so much better like that), when I run it I get a
Run-time error '13'
Type mismatch

Just to clarify, the worksheets have just numbers as names that is 1 - 2 - 3 etc not worksheet(1), is this the problem?
 
Upvote 0
Typo on
Code:
Dim Ws As Worksheets
It should be Worksheet (singular)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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