Link Command Button to VBA in another Workbook - excel

Have an Excel file for different days of the year. also have a master excel file. Would like to create a link button from the day excel files to the VBA in the Master file. So if need to change some code, I can change it in the master file and the day files will run the new code from the master file.

Assuming that, in an open workbook called "Master.xlsm", you have a macro called test:
Sub test()
MsgBox "in master"
End Sub
and that you have a forms control button on a sheet in another open workbook (perhaps "Daily.xlsx"), then you can assign the macro to that button as:
If you subsequently open just "Daily.xlsx" without first opening "Master.xlsm", you will get the
message (due to the link to "Master.xlsm"). Irrespective of whether you say "Update" or "Don't Update", "Master.xlsm" will be automatically opened when you click the button, and the test macro will be executed.

Related

Simulate button Cancel on Excel "Save changes" pop up

I'm trying to automate a SAP transaction using a VBA script. The transaction extracts some data to an Excel template, displays the resulting Excel file and for some reason tries to exit it which shows the "save changes" pop up window, this is where the transaction ends.
I want my VBA code to click "cancel" on the save changes pop up window.
I want my VBA execute this transaction on different items and save the resulting .xls files to a specific location, the problem is this pop up window stops the code and I have to click it manually.
Disabling the Pop up will not help as the file will exit without saving and will be lost. I don't have authorization to change the SAP code so my only option is to run a script through VBA.
To force a workbook to save changes, type the following code in a Visual Basic module of that workbook:
Sub Auto_Close()
If ThisWorkbook.Saved = False Then
ThisWorkbook.Save
End If
End Sub
This subprocedure checks to see if the file Saved property has been set to False. If so, the workbook has been changed since the last save, and those changes are saved.
SOURCE: https://support.microsoft.com/en-us/office/-how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel-189a257e-ec1b-40f7-9195-56d82e673071?ui=en-us&rs=en-us&ad=us

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

Export sheet including VBA and a command button

I need to save several Excel sheets to separate files.
The sheets are protected and locked, however I want to make spellcheck available.
This is possible with VBA using a small routine to unlock>spellcheck>relock
Sub SpellCheck()
ActiveSheet.Unprotect
Cells.CheckSpelling CustomDictionary:="CUSTOM.DIC", IgnoreUppercase:=False, AlwaysSuggest:=True, SpellLang:=1033
ActiveSheet.Protect
End Sub()
Now I placed command button on the sheets I want to export and assigned my spell check macro.
I save the files with vba as XLSM
Sheets("exportsheet").SaveAs Filename:="mysheet.xlsm", FileFormat:=52
If I click on the button in the newly saved file, the macro is linked to the original source excel which will open. The assigned macro link looks something like this: original_excel.xlsm!spellCheck()
How can I export a sheet including the VBA code which is assigned to a command button in a way that the macro is not assigned to the original workbook.
Any thoughts on that?
If you want the worksheet to be self contained after you export it from the workbook, make it self contained to start with.
Place all routines that are accessed from the sheet into that sheet's code module (instead of into a shared standard code module).
This way the sheet has no dependencies and will be self contained once exported to a new workbook.

Running an existing Macro on a new Excel File automatically when opened

(Excel 2010 question)
I have a reporting application that exports To excel. I have a Macro created that does some functionality. I need to run this macro whenever I open this new Excel Workbook after I export it.
Since it's a brand new worksbook, the excel does not have the macro that I have created embedded into it. So need to know if this is possible and how to do it.
Thanks!
You can open both the workbook that contains the macro and then the NEW workbook.
Then hit F11 for the Developer's Console. Usually it opens with the Project Explorer open on the left. If it isn't you can open it in the View menu, or by hitting Ctrl+R.
You need to copy the module that contains the macro into the modules of the New Workbook. Either insert a module from the Insert menu, or right click on the Modules > Insert > Module, then copy and paste the code, or you can just INSERT the module.
Let's assume your original Macro is named "Macro1". Make sure it isn't private.
I made one for example:
Sub Macro1()
MsgBox("Welcome to the Workbook!")
End Sub
In the Project Explorer, select the New workbook, and there should be an object titled "ThisWorkbook". Right click on that and select "View Code".
Private Sub Workbook_Open()
Call Macro1
End Sub
Save it, exit, open it back up and see your macro execute.

exec a xlam everytime I open an excel file

I have the following peace of code to exec a xlam file when I open the excel file:
Sub Auto_Open()
Application.OnTime Now + TimeValue("00:00:05"), "readCsv"
End Sub
Sub readCsv()
....
End Sub
I add it as Add-ins so every file I open has the xlam file on it. If there is no other excel file opened, it works perfectly but after that without closing the excel file I open a second one it does nothing. I need to exec the code even if there is another excel file opened. Is that possible?
I also try to do it writing the code in ThisWorkbook but the result is the same, If there is another excel file opened it does nothing.
To use your macro in any of the worksheet opened (irrespective of name) you can only possible do it by using a personal workbook Here is the Link
If the above doesnt suits you can prepare Add-ins and install it for users (Google for it if this is the case)
EDIT:
How to get add in in all opened files....
Goto File - Option - Quick Access Toolboar - From the dropdown "Choose Command from" - select Macros then select add-in macro - Add it, Below there would be a modify button Select the icon you like from it - OK
Now you will be able to see the icon with the addin function linked on top of the excel - click it for functioning, it will remain there forever ( if missed you can reapply the settings)....
The procedure are for 2010, for 2007 it should be similar....for 2003 there is a different way to achieve it...
Copy the .xlam to C:\Users[user]\AppData\Roaming\Microsoft\Excel\XLSTART. It will load every time you open Excel.

Resources