Need Macro to run on all Worksheets at same time

C

charles.w.price

I have a macro below that runs perfectly, but I would like it to run
on all worksheets and not just the active worksheet. The format on
each worksheet is identical, only the data differs. Could someone
help me add the extra code needed for the code to run on all
worksheets at the same time?

Thanks.

Sub SubTotal_All_Worksheets()

Dim intI As Integer
Dim intJ As Integer
Dim intK As Integer
Dim strA As String
Dim strB As String
Dim strFormula As String
Dim CDelta As String
Dim RStart As String
Dim CSum As String

CSum = InputBox("For which column do you wish to create sub-
totals?", "Sub-Total")
CDelta = InputBox("Create sums for each change in what column?",
"Column")
RStart = InputBox("What is the first row of data in that column?",
"Row")

intJ = 0
intK = 0
Range(CDelta & RStart).Select
For intI = 1 To 20000
strA = Range(CDelta & intI + RStart - 1 + intJ)
strB = Range(CDelta & intI + RStart + intJ)
If strA = strB Then
intK = intK + 1
Else:
Range(CDelta & intI + 2 + intJ).Select
Selection.EntireRow.Insert
strFormula = "=sum(" & CSum & (intI + RStart - 1 + intJ) & ":"
& CSum & (intI + RStart - 1 + intJ - intK) & ")"
Range(CSum & intI + RStart + intJ) = strFormula
Range(CSum & intI + RStart + intJ).Select
Selection.Font.Bold = True
intJ = intJ + 1
intK = 0
End If
Next intI

End Sub
 
J

jeff.j.griffith

Try this:

Dim objWorksheet As Worksheet

For Each objWorksheet In ActiveWorkbook.Worksheets
'Insert your code here and refer to objWorksheet
'i.e objWorksheet.Range(CSum & intI + RStart + intJ) =
strFormula
Next objWorksheet

That'll loop through all the worksheets for you.
 
C

charles.w.price

Try this:

Dim objWorksheet As Worksheet

For Each objWorksheet In ActiveWorkbook.Worksheets
'Insert your code here and refer to objWorksheet
'i.e objWorksheet.Range(CSum & intI + RStart + intJ) =
strFormula
Next objWorksheet

That'll loop through all the worksheets for you.

Could you give me a little more guidance? I can't seem to get it to
work.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top