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:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Which worksheet do you actually want to apply the filter to and which do you want to copy the results of the filter to?
 
Upvote 0
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)
 
Upvote 0
When you run the code is the sheet you want to filter the active sheet?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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