Stand-alone macro not working when added in "Add-in"

mortu90

New Member
Joined
Mar 1, 2016
Messages
8
Hi everybody,

I want to say that my experience with VBA is average. I cannot build full codes by myself, but I can modify most of the available temaplates
for my own purposes. But lets jump straight to my (weird) question:

I have developed a macro code which works absolutely fine from the workbook itself, but when I try to make it global (save it as an "add-in") I receive all sorts of errors. I believe that new "add-in" file has already a default "Sheet1" which perhaps messes up with my formulas. Besides that I have absolutely no clue why it works one way and not the other.


For Example:
Code:
Sub Copy_Header()
    ActiveSheet.Name = "Sheet1"
    Range("A1").EntireRow.Insert


    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual


    Dim wbCopy As Workbook
    Dim wsCopy As Worksheet
    Dim rngCopy As Range
    Dim wbPaste As Workbook
    Dim wsPaste As Worksheet
    Dim rngPaste As Range
    
    Set wbCopy = Workbooks.Open("Y:\Carriers\UPS\Header.xls")    'change path
    Set wsCopy = wbCopy.Worksheets("Header")
    Set rngCopy = wsCopy.Range("a1:ip1")
    Set wbPaste = ThisWorkbook
    Set wsPaste = wbPaste.Worksheets("Sheet1")  'paste to different sheet?
    Set rngPaste = wsPaste.Range("a1")  'change this if needed
    
    rngCopy.Copy
    rngPaste.PasteSpecial
    
    Application.DisplayAlerts = False
    
    Workbooks("Header.xls").Close


End Sub

The above macro (part of the full vba code) simply copy pastes a header from one document to another, which again works fine alone. When I try to make it global it simply doesn't.

Any input is much appreciated.

Regards,
M.
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
ThisWorkbook refers to the workbook containing the code, which is now your add-in, and I assume that is not where you want to paste to.
 
Upvote 0
Thanks for you reply RoryA! I already changed it from "ThisWorkBook" to "ActiveWorkBook". Perhaps I can trigger the right workbook in a different way. Also, whenever I add it as an "add-in" excel auto-creates a new worksheet named by default "sheet1".

The annoying part is that I already made it work, but I cannot share it with anybody as an add-in. :(
 
Upvote 0
I don't follow exactly what your problem is anymore?
 
Upvote 0
Hi RoryA,

Sorry for being a bit too vague. The problem was indeed as you suggested with the Workbook numbers. I didn't take into account that by adding a new "add-in" a new & hidden workbook is added. This eventually messed the other references.

Everything is solved now!

Thanks a lot for your help!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
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