Macro visibility in other excel sheets - vba

I created the macro in one of the excel workbook (abc.xlsm). And I want only that particular workbook to display the macros in view macros popup. But when view macros from other workbook (xyz.xlsx) I am able to view the macros which I created in the abc.xlsm.
I tried with private keyword before the macro. But it will hide the macro visibility in all the workbooks including the first workbook (abc.xlsm)
IS THERE ANYWAY TO RESTRICT THE MACRO VISIBILITY ONLY IN THE WORKBOOK WHICH IT WAS CREATED?

Excel macros popup lists all the macros available for execution in all workbooks that are open in the current Excel instance. This means that the only option to execute the macro only from a specific workbook is to check if ActiveWorkbook is the workbook you want the macro to be executed from. You can accomplish that with this line on top of your sub code:
If Not ActiveWorkbook.Name = "abc.xlsm" Then Exit Sub

Related

Excel 2013 VBA: Setting activeworkbook when clicking between workbooks

I have a workbook with a userform that contains a listbox that is used to populate data on a sheet.
If I have multiple workbooks open and I click from one workbook directly to the listbox on the userform in the other workbook, the ListBox_Change event fires before Activeworkbook changes to reflect the workbook that contains the userform. So when the code reaches Set EqDataSht = ActiveWorkbook.Worksheets("Equipment-Data") I get a subscript out of range error because the workbook I'm coming from doesn't contain a sheet named "Equipment-Data".
What is the best way to set the ActiveWorkbook to the parent of the userform? Thoughts I've had are setting a public variable wb = ActiveWorkbook on workbook_open or just trapping Err.Number=9 and telling the user to click on the sheet before clicking the userform. I'm sure there is something simple I am completely overlooking (VBA amateur).
Thoughts?
Instead of activeworkbook use thisworkbook which returns the workbook in which the code resides.
To make it active thisworkbook.activate should work

VBA Copying a macro and a worksheet which has a button assigned to the macro to another worksheet

I have an xlsm file which has a macro and worksheets. I need to copy the macro and a worksheet to a different workbook. Like to know the easiest way to do. Thanks!
Right-Click on the Sheet's tab and create a copy of the sheet in a new workbook.

Run macro on another worksheet from one sheet Excel

I have two Excel Workbooks (WB1, WB2). I want to be able to run a Macro from WB1 that will run a macro on WB2. The macro I want to run on Wb2 is already in WB2. Basically I want to click a button that will run the WB2 macro on WB2.
You can use Application.Run to run a macro on a different workbook. Assuming Wb2 is already open, and the macro you want to run is called MacroName then you can use the following ...
Application.Run "Wb2!MacroName"
from within Wb1
You can use ThisWorkbook which will reference the workbook you wrote the code in. A simple way to change this is to use the search function ctrl+f on your code and replace your workbook name with ThisWorkBook.
Credit to #3-14159265358979323846264 for catching my mistake.

Macro from on open event for worksheet

I have a macro that I want to run as part of open event. I recorded the macro to copy and paste some columns on a worksheet. This macro will only work when I am on that worksheet.
Since I want to run this macro as one of many applications when workbook is open and it doesn't always open on this specific worksheet I would like the code to target the worksheet and run the macro.
Here is my code but for some reason it doesn't work :
Application.Run "'Workbook.xlsb'!Sheet5.Copy_Paste2"
If I'm understanding you, you need to call Copy_Paste2 from ThisWorkbook Open event.
Private Sub Workbook_Open()
Sheet5.Copy_Paste2
End Sub

VBA Do not prompt to Save Macro

I have a VBA macro that makes prompts the user to save the changes to the Personal Macro Workbook each time it is run. Is there any way to disable this message?
I have tried this: ThisWorkBook.close SaveChanges:=Falsebut it still prompts the message
If there are no changes made to the Personal Macro Workbook, or if you don't want changes to be made, just use ThisWorkbook.Saved = True. You can also try setting Application.DisplayAlerts to False, call a close to the workbook, and turn the DisplayAlerts to True again.
The above can be seen here.
If you have formulas in a worksheet in the personal macro workbook, Excel will automatically recalculate them every time the workbook is opened and this constitutes a change which Excel will ask you to save.
Get round it by not putting formulas in the worksheets in the personal macro workbook. If your macro requires formulas, you could always create these in another workbook which your macro can access when it runs.