I have some code which whenever a sheet is accessed is copies the contents from a master worksheet and then applies an auto filter based upon the worksheet name.
The master sheet is call "All Features" and everything is copied off this. Then the filter code strips out the non-numeric characters from the sheet name and uses that as it's auto-filter. All the sheets are called something like "Inc 301" or "Inc201" for instance.
What would be better is if the filter could be applied to the "All Features" sheet before the copy is done, but when I tried this I could not get it to happen.
Here is the original code:
I thought I could add the filter to the:
bit but that doesn't seem to do anything.
any help would be appreciated.
The master sheet is call "All Features" and everything is copied off this. Then the filter code strips out the non-numeric characters from the sheet name and uses that as it's auto-filter. All the sheets are called something like "Inc 301" or "Inc201" for instance.
What would be better is if the filter could be applied to the "All Features" sheet before the copy is done, but when I tried this I could not get it to happen.
Here is the original code:
Code:
Public Sub AllIncs()
ActiveSheet.unprotect ("inform")
Dim LastRow As Long
Dim FirstRow As Long
Dim LastCol As Integer
Dim FirstCol As Integer
' Find the FIRST real row
FirstRow = Sheets("All Features").Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByRows).Row
' Find the FIRST real column
FirstCol = Sheets("All Features").Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByColumns).Column
' Find the LAST real row
LastRow = Sheets("All Features").Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
' Find the LAST real column
LastCol = Sheets("All Features").Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
'Select the ACTUAL Used Range as identified by the
'variables identified above
With Worksheets("All Features")
.unprotect
.Range(.Cells(FirstRow, FirstCol), .Cells(LastRow, LastCol - 1)).Copy _
Destination:=Range("A1")
End With
'Get Filter by sheetname
Dim SheetName As String
SheetName = Mid(ActiveSheet.Name, 4, Len(ActiveSheet.Name))
If InStr(SheetName, " ") = 0 Then
RemoveSpaces = SheetName
Else
SheetName = Left(SheetName, InStr(SheetName, " ") - 1) _
& Right(SheetName, Len(SheetName) - InStr(SheetName, " "))
End If
'Make Sure the sheet is a normal Inc Sheet
If Mid(ActiveSheet.Name, 1, 3) = "Inc" Then
Range("A1").Select
Selection.AutoFilter Field:=6, Criteria1:=SheetName
Else
Selection.AutoFilter Field:=6, Criteria1:="REPLACE THIS"
End If
Cells(1, 1).Select
ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True
End Sub
I thought I could add the filter to the:
Code:
With Worksheets("All Features")
.unprotect
.Range(.Cells(FirstRow, FirstCol), .Cells(LastRow, LastCol - 1)).Copy _
Destination:=Range("A1")
End With
bit but that doesn't seem to do anything.
any help would be appreciated.