Run Addin macro on Open workbook

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have a macro-add in where it will attached the files on another already open workbook. However, whenever I run the codes, the files are being attached to the macro add-in and not on the open workbook.
I have this section that I think I need to tweak the codes. Otherwise, feel free to let me know other ways.
Code:
Private Sub OpenFile(filepath)
    Dim sh, wb As Workbook, wbMe As Workbook
    Set wbMe = ThisWorkbook
    Set wb = Workbooks.Open(filepath)
        For Each sh In wb.Sheets
            sh.Copy after:=wbMe.Sheets(wbMe.Sheets.Count)
        Next sh
    wb.Close False
End Sub

Note: The open workbook has a word "Master" on its filename (as the name varies based on subject like Math Master 2018).


Any thoughts will be appreciated.
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
ThisWorkbook always refers to the workbook with the code in it - i.e. your add-in. You need to use ActiveWorkbook instead:

Code:
Set wbMe = ActiveWorkbook
 
Upvote 0

Forum statistics

Threads
1,215,661
Messages
6,126,091
Members
449,290
Latest member
mrsbean

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