P
PHisaw
Hi,
I have a workbook where data is compiled through out the year and has to be
re-ran from the beginning of the year each month due to changes in open
orders. I also have worksheets breaking down each month, but it has become a
problem recreating the worksheets each month. I have code (from Ron de
Bruin) to create a worksheet for each month ("01-09", "02-09", etc.). Now, I
want to save the workbook (ActiveWorkbook.Save) after the pages are created
and run the following code for worksheets named 01-09, 02-09, etc. This will
work for one worksheet, but I can't seem to find the right code to make it
work for an array of worksheets.
It will sort and subtotal specified columns and then bold total rows and
insert a row after each total row.
Sub Total_Worksheets()
Dim rng As Range
Range("A1900").Select
Selection.Sort Key1:=Range("c2"), Order1:=xlAscending, _
Key2:=Range("A2"), Order2:=xlAscending, _
Key2:=Range("b2"), Order2:=xlAscending, _
Header:=xlYes
With Sheets("02-09")
On Error Resume Next
Set rng = Range(Range("j2"), Cells(2, Columns.Count).End(xlToLeft))
On Error GoTo 0
If Not rng Is Nothing Then
.Range("j2").Subtotal _
GroupBy:=3, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
.Range("j2").Subtotal _
GroupBy:=1, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
.Range("j2").Subtotal _
GroupBy:=2, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
End If
End With
Dim LastRow As Long
Dim r As Long
LastRow = Range("G" & Rows.Count).End(xlUp).Row
For r = LastRow To 2 Step -1
If InStr(1, Cells(r, 1).Value, "Total") > 0 Or _
InStr(1, Cells(r, 2).Value, "Total") > 0 Or _
InStr(1, Cells(r, 3).Value, "Total") > 0 Or _
InStr(1, Cells(r, 4).Value, "Total") > 0 Then
Range(Cells(r, 1), Cells(r, 30)).Font.Bold = True
ActiveSheet.Rows(r + 1).EntireRow.Insert
End If
Next
End Sub
If this is possible to do over multiple sheets, I would really appreciate
code to make it work.
Thanks in advance,
Phisaw
I have a workbook where data is compiled through out the year and has to be
re-ran from the beginning of the year each month due to changes in open
orders. I also have worksheets breaking down each month, but it has become a
problem recreating the worksheets each month. I have code (from Ron de
Bruin) to create a worksheet for each month ("01-09", "02-09", etc.). Now, I
want to save the workbook (ActiveWorkbook.Save) after the pages are created
and run the following code for worksheets named 01-09, 02-09, etc. This will
work for one worksheet, but I can't seem to find the right code to make it
work for an array of worksheets.
It will sort and subtotal specified columns and then bold total rows and
insert a row after each total row.
Sub Total_Worksheets()
Dim rng As Range
Range("A1900").Select
Selection.Sort Key1:=Range("c2"), Order1:=xlAscending, _
Key2:=Range("A2"), Order2:=xlAscending, _
Key2:=Range("b2"), Order2:=xlAscending, _
Header:=xlYes
With Sheets("02-09")
On Error Resume Next
Set rng = Range(Range("j2"), Cells(2, Columns.Count).End(xlToLeft))
On Error GoTo 0
If Not rng Is Nothing Then
.Range("j2").Subtotal _
GroupBy:=3, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
.Range("j2").Subtotal _
GroupBy:=1, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
.Range("j2").Subtotal _
GroupBy:=2, _
Function:=xlSum, _
TotalList:=Array(10, 11, 12), _
Replace:=False, _
PageBreaks:=False, _
SummaryBelowData:=True
End If
End With
Dim LastRow As Long
Dim r As Long
LastRow = Range("G" & Rows.Count).End(xlUp).Row
For r = LastRow To 2 Step -1
If InStr(1, Cells(r, 1).Value, "Total") > 0 Or _
InStr(1, Cells(r, 2).Value, "Total") > 0 Or _
InStr(1, Cells(r, 3).Value, "Total") > 0 Or _
InStr(1, Cells(r, 4).Value, "Total") > 0 Then
Range(Cells(r, 1), Cells(r, 30)).Font.Bold = True
ActiveSheet.Rows(r + 1).EntireRow.Insert
End If
Next
End Sub
If this is possible to do over multiple sheets, I would really appreciate
code to make it work.
Thanks in advance,
Phisaw