vba For Loop using AutoFilter with Variable Criteria

rmagedyn

New Member
Joined
Aug 6, 2012
Messages
7
I'm trying to automate several daily reports, and have run into a snag on my 1st go at it :confused:... I used the Macro Recorder to lay the groundwork, and then consolidated the repetitive steps in a For Each/Next loop. It begins with 1 sheet that needs to be cut and pasted across up to 5 sheets. I found a lot of useful info across the web, and put together the following:

Code:
Sub Daily_Pending()
'
' Daily_Pending Macro
' Daily Pending Report Macro
'
'


Dim Region As Variant
    
Call CriteriaList
    Columns("AU:AV").Select
    Selection.Delete Shift:=xlToLeft
    ActiveSheet.Range("A1:AF1").AutoFilter
    On Error Resume Next
    For Each Region In Worksheets("Sheet1").Range("AU2:AU6").Cells
        Range("A1").Select
        Call LCell
[COLOR=#ff0000]        ActiveSheet.Range("$A$1:$AF$" & LastCell.Row).AutoFilter Field:=1, Criteria1:= _[/COLOR]
[COLOR=#ff0000]            "=" & Region.Value   [/COLOR] [COLOR=#008000]<=== Variable for Criteria1 - Apparently this line is ignored[/COLOR]
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
        Sheets.Add After:=Sheets(Sheets.Count)
        On Error Resume Next
[COLOR=#ff0000]        ActiveSheet.Name = Region.Value[/COLOR]
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        Application.CutCopyMode = False
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With ActiveWindow
            .SplitColumn = 1
            .SplitRow = 0
        End With
        ActiveWindow.FreezePanes = True
        Columns("A:A").ColumnWidth = 20
        Columns("B:GG").Select
        Columns("B:GG").EntireColumn.AutoFit
[COLOR=#ff0000]        If Columns("B:GG").EntireColumn.ColumnWidth > 48 Then[/COLOR]
[COLOR=#ff0000]            Columns("B:GG").EntireColumn.ColumnWidth = 48[/COLOR]
[COLOR=#ff0000]        End If[/COLOR]
        Cells.Select
        Cells.EntireRow.AutoFit
        Range("B1").Select
        Sheets("Daily Pending _ Afte").Select
        Rows("2:2").Select
        Range("A2").Activate
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Delete Shift:=xlUp
    Next Region

The code works without error, creates the 6 additional sheets (1 for the Range values), but it doesn't name them. It copies and transposes the entire contents of the original sheet to the second sheet created rather than filtering it based off the range value. It also doesn't seem to format the column widths to a max of 48 :( Is this even possible? Below is the code for the sub and function that this sub calls (All three are in separate modules):

Code:
Sub CriteriaList()


Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Daily Pending _ Afte").Select


With ActiveSheet
    .Range("A1", .Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Sheet1").Range("A1"), Unique:=True
    Sheets("Sheet1").Range("A1").Delete Shift:=xlShiftUp
End With


End Sub



Code:
Public Function LCell()
Dim LastCell As Range
Dim LastCellRowNumber As Long


Set WS = Worksheets("Sheet1")
With WS
    Set LastCell = .Cells(.Rows.Count, "AF").End(xlUp)
    LastCellRowNumber = LastCell.Row
End With
Return
End Function
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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