Struggliing with relative / variable filenames and data ranges.

wittonlin

Board Regular
Joined
Jan 30, 2016
Messages
144
All I want, as with so many macros; I just want them to run on the ActiveSheet, regardless of it's name.

I can even use ActiveSheet.Name to print a popup of the actual name of a particular Workbook/sheet, so I know the Macro knows what the name of the document is, but in no way can I include it in the code unless I include the EXACT filename.

I've tried many different ideas with ActiveSheet, GetActiveSheet, OpenFileName, Sheets(ActiveSheet.Name), ThisWorkbook.ActiveSheet etc.

E.g.
With Worksheets("CurrentFileUsing")

Unless the file name is CurrentFileUsing I can't get this simple Sub to run properly.

Funny, if I just use Worksheets() it gets past that point, but then errors on the next line, seemingly expecting (Set r = Range without the period: .Range)

Code:
Sub MoveNonStatesToSheet1()

Sheets.Add After:=Sheets(Sheets.count) 
Dim r As Range, filtr As Range
With Worksheets("CurrentFileUsing")
Set r = .Range("A1").CurrentRegion

On Error Resume Next
r.AutoFilter field:=.Range("F1").Column, Criteria1:="KY"
Set filtr = r.SpecialCells(xlCellTypeVisible)
'MsgBox filtr.Address
Set filtr = r.Offset(1, 0).Resize(r.Rows.count - 0).SpecialCells(xlCellTypeVisible)
'MsgBox filtr.Address
filtr.Copy
With Worksheets("Sheet1")
.Cells(Rows.count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
filtr.EntireRow.Delete
r.AutoFilter

End Sub
There's more AutoFilters but just this as an example.
 
Last edited:

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,539
Office Version
365
Platform
Windows
Which worksheet do you actually want to apply the filter to and which do you want to copy the results of the filter to?
 

wittonlin

Board Regular
Joined
Jan 30, 2016
Messages
144
Apply the filter to the first worksheet of a workbook called CurrentFileUsing, CurrentFileUsing2, CurrentFileUsing3, CurrentFileUsing4, or a different name all together.

Then the data is always copied to whatever the next sheet is, almost always sheet1.
Sheets.Add After:=Sheets(Sheets.count)
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,539
Office Version
365
Platform
Windows
When you run the code is the sheet you want to filter the active sheet?
 

wittonlin

Board Regular
Joined
Jan 30, 2016
Messages
144
The Macro below works, but I can't for the life of me figure out how to use multiple state codes as the Criteria1.

This deals perfectly with using any Active Sheet or Book, but when I need more states than KY like MO, TN, etc. I can't figure it out. I have another Macro but it takes each state and adds the row data for each matching Criteria1 in progressive Sheets! E.g. Criteria1 KY those rows go to Sheet1, but MO Criteria rows cut to Sheet 2, TN to Sheet 3 and so on.

Code:
Sub MoveNonStatesToSheet1() 
     
    Dim r As Range, filtr As Range, ws As Worksheet 
     
    With ActiveSheet 
        Set r = .Range("A1").CurrentRegion 
         'On Error Resume Next
        r.AutoFilter field:=.Range("F1").Column, Criteria1:="KY" 
        Set filtr = r.SpecialCells(xlCellTypeVisible) 
         'MsgBox filtr.Address
        Set filtr = r.Offset(1, 0).Resize(r.Rows.Count - 0).SpecialCells(xlCellTypeVisible) 
         'MsgBox filtr.Address
        Set ws = Sheets.Add(After:=Sheets(Sheets.Count)) 
        filtr.Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 
    End With 
     
    filtr.EntireRow.Delete 
    r.AutoFilter 
     
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,095,415
Messages
5,444,334
Members
405,278
Latest member
karen1

This Week's Hot Topics

Top