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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,459
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,459
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
 

Forum statistics

Threads
1,089,570
Messages
5,409,064
Members
403,248
Latest member
KOH2031

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top