Naming New Worksheets During Loop

Joe_L

New Member
Joined
Mar 10, 2011
Messages
14
Hi all - below is a loop that cycles through 10 "check" columns in a spreadsheet named "Template", selects the "check" item in the filter for each column, and pastes the results to a new spreadsheet for all 10 checks (10 new worksheets). What's driving me crazy is trying to figure out a way to name each new tab based on the column heading of each check. This would be range(CG1:CP1). Is there anyway to add in this range to my code and insert a naming command after "worksheets.add"? Right now it appears as "sheet1", "sheet2", "sheet3", etc. Thanks so much for any help


Sub Data_Check_Report()
Application.ScreenUpdating = False
Dim LastCol As Integer
Dim c As Integer
LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
For c = 85 To LastCol
ActiveSheet.Range("A:$CP").AutoFilter Field:=c, Criteria1:="CHECK"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets.Add
ActiveSheet.Paste
Columns("A:CP").EntireColumn.AutoFit
Sheets("Template").Select
ActiveSheet.Range("A:CP").AutoFilter Field:=c
Next c

Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Welcome to MrExcel.

Try (untested):

Rich (BB code):
Sub Data_Check_Report()
    Dim Sh As Worksheet
    Set Sh = ActiveSheet
    Application.ScreenUpdating = False
    Dim LastCol As Integer
    Dim c As Integer
    LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
    For c = 85 To LastCol
        ActiveSheet.Range("A:$CP").AutoFilter Field:=c, Criteria1:="CHECK"
        Range("A1").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Worksheets.Add
        ActiveSheet.Paste
        ActiveSheet.Name = Sh.Cells(1, c).Value
        Columns("A:CP").EntireColumn.AutoFit
        Sheets("Template").Select
        ActiveSheet.Range("A:CP").AutoFilter Field:=c
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
if I may just ask one more quick question, I just noticed that at times there are some columns where the filter criteria never equals "CHECK".

Is there a way to ammend the above code so that I can exclude those columns from the loop?

I've been toying around with various ideas but can't get it to execute correctly. thanks again-
 
Upvote 0
Yes - and when the below code tries to execute the copy and paste function, it selects far too many rows and causes me to receive a "excel cannot complete task with available resources" message.

Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
 
Upvote 0
So test for that and don't copy if it's True:

Code:
    If Selection.Rows.Count > 1 Then
        Selection.Copy
        Worksheets.Add
        ActiveSheet.Paste
        ActiveSheet.Name = Sh.Cells(1, c).Value
        Columns("A:CP").EntireColumn.AutoFit
        Sheets("Template").Select
    End If
 
Upvote 0
Thanks for the idea. Seems like it should work but unfortunitely even though there is only one row of headers when no criteria is present, it looks to be selecting a huge number of blank rows. Conversly when I try to write as:

If Selection.Rows.Count < 500

I still get the same result. Maybe because the rows are all completely blank?
 
Upvote 0
That's because you are using End(xlDown). Try:

Code:
Sub Data_Check_Report()
    Dim Sh As Worksheet
    Set Sh = ActiveSheet
    Application.ScreenUpdating = False
    Dim LastCol As Integer
    Dim c As Integer
    LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
    For c = 85 To LastCol
        With ActiveSheet.Range("A1").CurrentRegion
            .AutoFilter Field:=c, Criteria1:="CHECK"
            If .SpecialCells(xlCellTypeVisible).Cells.Count > .Columns.Count Then
                .Copy
                Worksheets.Add
                ActiveSheet.Paste
                ActiveSheet.Name = Sh.Cells(1, c).Value
                Columns("A:CP").EntireColumn.AutoFit
                Sheets("Template").Select
            End If
            .AutoFilter Field:=c
        End With
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Wow, works exactly how I was picturing. Thanks very much for the help as I would not have known how to write that. thanks again.

- Joe
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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