Copy/paste macro so that it works for active sheet
Results 1 to 5 of 5

Thread: Copy/paste macro so that it works for active sheet

  1. #1
    Board Regular
    Join Date
    Jul 2019
    Location
    Win 10, Office Professional Plus 2016
    Posts
    106
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Copy/paste macro so that it works for active sheet

    Code:
    Sub TEST()
    '
    ' TEST Macro
    '
    '(Deleting whatever data is in LastSheet)
    
     Sheets("LastSheet").Select
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Delete Shift:=xlUp
        Sheets("Sheet1").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"). _
        Sheets("Sheet1").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
    
    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 by danhendo888; Aug 4th, 2019 at 05:40 AM.

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,989
    Post Thanks / Like
    Mentioned
    460 Post(s)
    Tagged
    45 Thread(s)

    Default Re: Copy/paste macro so that it works for active sheet

    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
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    Board Regular
    Join Date
    Jul 2019
    Location
    Win 10, Office Professional Plus 2016
    Posts
    106
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/paste macro so that it works for active sheet

    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 by danhendo888; Aug 4th, 2019 at 07:28 PM.

  4. #4
    Board Regular
    Join Date
    Jul 2019
    Location
    Win 10, Office Professional Plus 2016
    Posts
    106
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Copy/paste macro so that it works for active sheet

    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.

  5. #5
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    26,989
    Post Thanks / Like
    Mentioned
    460 Post(s)
    Tagged
    45 Thread(s)

    Default Re: Copy/paste macro so that it works for active sheet

    Try changing
    Code:
    .UsedRange.EntireRow.Delete
    to
    Code:
    .UsedRange.ClearContents
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •