Link to home
Start Free TrialLog in
Avatar of gregfthompson
gregfthompsonFlag for Australia

asked on

Extend Excel macro across multiple worksheets

The macro retrieves specific rows from a single worksheet. The objective is to have the macro work across all worksheets within the file. See attached file with macro attached to sheet 1.
2011-Australia-ailments-macro.xlsm
Avatar of Shanan212
Shanan212
Flag of Canada image

Sub sample()

Dim wscounter As Long, rrow As Long, i As Long
Dim curWs As String, Main As Worksheet, irow As Long

Set Main = Sheets("Main")

wscounter = ActiveWorkbook.Worksheets.Count

    For i = 1 To wscounter
        ActiveWorkbook.Worksheets(i).Select
        curWs = ActiveWorkbook.Worksheets(i).Name
        
        If (curWs <> "Main") Then
            irow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
            Range("A4:N" & irow).Copy
            
            rrow = Main.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row
            
            Main.Range("A" & rrow + 1).PasteSpecial xlPasteValuesAndNumberFormats

            ActiveSheet.CutCopyMode = False
        End If
    Next i

End Sub

Open in new window


Should paste on sheet called 'Main'

So make a sheet called main and call the sub from there.
ASKER CERTIFIED SOLUTION
Avatar of Jignesh Thar
Jignesh Thar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gregfthompson

ASKER

jigneshthar,

Thanks for your question.

I want to be able to amend "MyCodes = Array(2621)" to a specific postcode and then have each worksheet show only the rows that contain that postcode.

Does this make sense?

Thanks,

Greg
Oops. My error.  It already works as required.