loop through selected tab

dmac13

New Member
Joined
Oct 21, 2010
Messages
30
Hi

Is there a way to loop through a list of tabs individually ? i want to add subtotals to certain sheets, i know it is not possible to add subtotals to multilple sheets at once so i want the macro to loop though the sheets idividually.

so far i have, but this errors at the subtotal


sub macro_x()

Dim MyTab As Object
Dim LastRow As String

Sheets("TY COMPANY SUM").Select
LastRow = Range("B4").End(xlDown).Row

'Add grage lookup, sort and add subtotals to the TY COMPANY SUM

Sheets(Array("TY COMPANY SUM", "LY COMPANY SUM")).Select

For Each MyTab In ActiveWindow.SelectedSheets
MyTab.Activate

Range("D5").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],LFL!R4C1:R1000C3,3,0)"
Selection.AutoFill Destination:=Range("D5:D" & LastRow), Type:=xlFillDefault

Columns("D:D").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete

Range("D5").Select
Range("B4:F568").Sort Key1:=Range("D5"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(4, 5), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
ActiveSheet.Outline.ShowLevels RowLevels:=2

Next mytab
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
so could you explain what your macro is suppose to do. Because it seems like you have a vlookup formula created and copied down, then you delete those same rows?

also this line
Range("B4:F568").Sort Key1:=Range("D5")
don't you want it to include the "LastRow" instead of the listed row 568?
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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