Copy/paste macro so that it works for active sheet

danhendo888

Board Regular
Joined
Jul 15, 2019
Messages
167
Office Version
  1. 365
Platform
  1. 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:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
How about
Code:
Sub danhendo888()
   Dim Ws As Worksheet
   Dim Wbk As Workbook
   
   Set Ws = ActiveSheet
   With Sheets("LastSheet")
      .UsedRange.EntireRow.Delete
      Set Wbk = Workbooks.Open("C:\mrexcel\+book1.xlsm") '("C:\Users\User\Downloads\SecondWorkbook.XLSX")
      Wbk.Sheets(1).Range("A1:EM24953").AdvancedFilter Action:= _
         xlFilterCopy, CriteriaRange:=Ws.Range("A1:A2"), CopyToRange:=.Range("A1"), Unique:=False
   End With
End Sub
 
Upvote 0
Hi Fluff,

Thank you for the code, I appreciate it :)

My sheets (Sheet1, Sheet2..) in FirstWorkbook have formulas that reference the data from LastSheet (which is where we are pasting the data).
When we delete all rows in LastSheet before pasting the data, it produces REF errors in Sheet1, Sheet2 etc

I'll try to adjust the code to:
- delete from rows 2 and below in LastSheet
- copy and paste the data in SecondWorkbook to cell A2 and below in LastSheet
 
Last edited:
Upvote 0
Also, would it help things to convert the data in SecondWorkbook to a table (say, Table2)?

FWIW the data in LastSheet of FirstWorkbook should maintain table formatting (Table1) for my formulas on Sheet1, Sheet2.. to work.
 
Upvote 0
Try changing
Code:
.UsedRange.EntireRow.Delete
to
Code:
.UsedRange.ClearContents
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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