danhendo888
Board Regular
- Joined
- Jul 15, 2019
- Messages
- 167
- Office Version
- 365
- Platform
- Windows
Code:
[FONT=Verdana]Sub TEST()
'
' TEST Macro
'[/FONT]
[FONT=Verdana]'[U](Deleting whatever data is in LastSheet)
[/U]
Sheets("LastSheet").Select
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
[COLOR=#ff0000]Sheets("Sheet1")[/COLOR].Select
Range("A1").Select
Workbooks.Open "C:\Users\User\Downloads\SecondWorkbook.XLSX"
Windows("SecondWorkbook.XLSX").Activate
Range("A2").Select
Windows("FirstWorkbook.xlsm").Activate
Range("A1").Select
Windows("SecondWorkbook.XLSX").Activate
Application.CutCopyMode = False
Application.CutCopyMode = False
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveCell.Offset(-1, 0).Range("A1:EM24953").AdvancedFilter Action:= _
xlFilterCopy, CriteriaRange:=Workbooks("FirstWorkbook.xlsm"). _
[COLOR=#ff0000]Sheets("Sheet1")[/COLOR].Range("A1:A2"), CopyToRange:=ActiveCell.Offset(24953, 0). _
Range("A1"), Unique:=False
Windows("FirstWorkbook.xlsm").Activate
Windows("SecondWorkbook.XLSX").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Windows("FirstWorkbook.xlsm").Activate
Sheets("LastSheet").Select
Range("A2").Select
ActiveSheet.Paste
End Sub
[/FONT]
I am working from two Workbooks:
FirstWorkbook; and
SecondWorkbook
FirstWorkbook has 20 sheets, named Sheet1, Sheet2,.. and LastSheet
SecondWorkbook has one sheet of data
I have created a macro in Sheet1 (of FirstWorkbook) that opens SecondWorkbook, does an advanced filter using criteria range of A1:A2 of Sheet1 and then pastes the data into LastSheet in FirstWorkbook. This is the code pasted above.
The macro will essentially be the same for all other sheets in FirstWorkbook since A1:A2 of all the sheets will be the Criteria range for the advanced filter.
Instead of creating a new macro for each sheet by replacing Sheet1 with Sheet2 etc for every sheet, is there a way to change the code so that it works for the "active sheet"?
Last edited: