VBA Do not prompt to Save Macro - vba

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.

Related

How to keep unrelated workbooks open in excel when running a macro

I am writing a macro in VB in excel 2013. The only remaining bug left is when the macro runs, all of the other workbooks i have open close, even ones unrelated to the macro. I do not want users of my macro to lose other tabs of excel they may have open if they run my program. I have tried using "Application.Visible = true" but it is not giving me the desired behavior. Is there a way to run a macro without affecting any other currently open workbooks? Or is it possible that something else I have written is overriding the Application.Visible method? If someone could provide a recommendation it would be much appreciated.
Application.Visible = false
Simply does what it says, set the application "Excel in your case" to invisible.
If you want to hide something specific you have to call this:
e.g Sheet
Sheets("Whatever").Visible = False
e.g Workbook
ActiveWorkbook.Windows(1).Visible = False
//ActiveWorkbook needs to have an reference to the specified Workbook which you get as an example if you store the opening of the workbook (ActiveWorkbook = Workbooks.Open )
It sounds like you have a line
Workbooks.close
(which will close all workbooks) when what you wanted to do was close the specific workbook with the macro. That should be
ThisWorkbook.Close

Run Macro from Desktop Shortcut

I wrote a macro that does some calculations based on a particular type of excel sheet. Im trying to distribute this to my coworkers but the addition of a macro to a workbook and then running the macro is something foreign to them. I'd like to have a "shortcut" or some VBS program to open a specific workbook (specified by the user), run the macro, and display the results.
Your help is appreciated!
--Edit--
I wrote a macro in VBA. I exported the file to my desktop. Its simply called "Macro1". We have a standard form of excel sheet our company uses. Its literally the same sheet with different numbers. The macro I designed works on these kinds of sheets and does calculations. My coworkers aren't good with macros, so I want some sort of "code" that will prompt one of my coworkers for an excel file, then execute the macro on the file. Hopefully this clarifies any questions.
You need to make it a excel add-in.
Then in the add-in make it run on workbook open with Sub App_SheetActivate(ByVal Sh As Object) in thisworkbook.
In the macro you can then have it only activate on certain workbook name or workbook type by:
If range("A1").value = "something" ' something that makes the workbook type special.
' Maybe you need B1 value and so on too.
Do you need a way to self-install the add in just let me know and I have a code for that too.
Self install:
' if add-in is not installed and the workbook is a add-in (workbookcount =0)
' Also take note that this code will only run if the add-in is not installed
If Dir(Application.UserLibraryPath & "YourWorkbookName.xlam") = "" And Workbooks.Count = 0 Then
'optional ask user if he wants to install or not. Code not included.
' copy file from current position to add-ins folder.
Result = apiCopyFile(ThisWorkbook.FullName, Application.UserLibraryPath & "YourWorkbookName.xlam", False)
' activate the add-in
AddIns("YourAdd-inName").Installed = True
msgbox("add-in installed")
' Close Excel since add-ins does not work without restart of Excel
On Error Resume Next
Application.Interactive = False
AppActivate "Microsoft Excel"
Application.Quit
Exit Sub
End If
Note that the file must be saved as a add-in. (xlam) this means there is no sheets, the workbook is VBA code only.
Normally, that does not mean the code needs to be written in a special way.
Usually Range("XX").value works, but some commands may need to point towards the correct workbook. (you have two workbooks open with add-ins, the add-in with the code and the workbook with the sheets and numbers)
Hope this helps

What is the command in Excel VBA to delete a specific macro?

I have an Excel Macro Template. I run a macro on it and it saves the new spreadsheet as an xlsx instead.
I'm just wondering if there's a simple command for VBA that just deletes a macro by name.
I've researched this a lot, and there are basically two answers which don't really fit.
The first is to not have macros in the workbook I'm saving and to just run macros from one workbook to another. (I don't want to do this for a few reasons, but simplicity is the main reason.)
The second is a VBA script that strips ALL VBA and connections from the workbook. (I don't need it to do that much, and I'd rather just delete the one macro I have.)
So, what I'm looking for is just something like this:
Delete.Macro("Import") 'This command deletes the macro in this workbook named "Import".
try
Sub main()
Application.DisplayAlerts = False
Workbooks("PutWorkBookName").SaveAs FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
Fun thought, use VBA to remove VBA... but seems more work than necessary.
What about activeworkbook.saveas "filename.xlsx", xlExcel12?
As I am sure you are aware, an .xlsx doesn't have any code, so if you set warnings to false, etc., it will just save the file and delete the code.
Now, if I misread, and you want to delete only some of your code, leave the rest, and save as xlsx... you are out of luck.

Turn On Automatic Calculations

I am using Excel 2013 and we are coming across random Excel files set to manual calculations and they do not seem to go away after resetting to automatic.
Those files seem to stay as automatic, but on a random day a different Excel file or the same Excel reverts back to manual. I want to auto execute a macro when loading any Excel file or just the program to set Excel to automatic calculations.
I tried the following macro:
Private Sub Auto_Open()
Application.Calculation = xlCalculationAutomatic
I received the following error message upon loading Excel:
"Run-time error '1004': Method 'Calculation' of object'_Application'failed
Troubleshoot:
An Auto_Open macro runs before any other workbooks open. Therefore, if you record actions that you want Excel to perform on the default Book1 workbook or on a workbook that is loaded from the XLStart folder, the Auto_Open macro will fail when you restart Excel, because the macro runs before the default and startup workbooks open.
If you encounter these limitations, instead of recording an Auto_Open macro, you must create a VBA procedure for the Open event as described in the next section of this article.
Question: is there a way to create a macro to reset any Excel file back to Automatic? I stored the macro in my personal workbook as I am hoping that the macro will execute on any Excel file I load.
I tried what you wrote and it works.
Just in case, here is my code :)
Private Sub auto_open()
Application.Calculation = xlAutomatic
End Sub

Macro visibility in other excel sheets

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