Saving file with macro - vba

I entered the following code into Visual Basic.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("$D$10:$S$29")) Is Nothing Then
ActiveWindow.Zoom = 80
Else
ActiveWindow.Zoom = 100
End If
End Sub
It works well to zoom in when a certain range is selected, and to zoom out when anywhere else on the page is selected.
When I go to save the workbook, the following message comes up:
"The following features cannot be saved in macro-free works:
- VB project
To save a file with these features, click No, and then choose a macr-enabled file type in the File Type list.
To continue saving as a macro-free workbook, click Yes."
When saving I have changed the "save as type" to "Excel Macro-Enabled Workbook". This saves the text in the editor, but when I re-open the file, the macro doesn't work.
I have also saved the file without the macro entered as an "Excel Macro-Enabled Workbook" and then copied the macro into the workbook, checked to see if the macro works (it does), then save the file, close and re-open.
The say outcome of the macro being saved but not working.
How can I save this workbook so that the macro is saved with it and works each time I open the file?
Thank you very much in advance for your help.

#user2879835, After you saved the workbook as "Excel Macro-Enabled Workbook" and while opening the same does it asking any security warnings like "Macros have been disabled"?...and also Worksheet_Change occur when cells on the worksheet either changed by the user or by any link.

Related

VBA Ribbon Bar Integration

I have an Excel macro that performs a few functions on a document (Creates as form, and a few emails) all from MS-Word documents. If the macro is executed from the main spreadsheet (where the macro is), everything works normally. I want to place this macro on the ribbon allowing a user to launch it without having (or knowing where the main excel document is located or having it open). I created a sub to check to see if the spreadsheet was open and modified the ribbon to include an icon for the macro.
Which works. However, when launched from the ribbon while the main Excel spreadsheet is not open, it opens the workbook and runs the macro in entirety (Without executing the open workbook line of the macro). I assume the spreadsheet is being open because the macro that is being called resides with it (makes sense). Since the macro is dependent on the data contains in the spreadsheet, I need to allow the users to modify it and then re-running the macros from the ribbon again.
Does anyone have a recommended approach or best practices? Thank you in advance.
Sub MainForm()
Dim WorkingFolder As String
Dim File01 As String 'Main Excel Data File, where all data is
Dim File02 As String 'Preliminary Email to send to user
Dim File03 As String 'Final Email to Send to user when production is complete
Dim wb As Workbook
WorkingFolder = "C:\Temp\"
File01 = "01-MainData.xlsm"
File02 = "02-PreProductionEmail.docx"
File03 = "03-FinalProductionEmail.docx"
If wbIsOpen(File01) = True Then
MsgBox "Workbook Is Open"
Run ("'C:\Users\Guest\Nextcloud\Documents\Excel Forms\02-TEST-Production Request-Data.xlsm'!CreateProductionForm")
Else
MsgBox "The Main Datafile is not open, verify the last row before re-runing", vbOKOnly, "Not Open"
Set wk = Workbooks.Open(WorkingFolder & DataFile)
End If
End Sub
The Guide mentioned by Ricardo was not quite what I needed but it was helpful and did put me on the right track. Thank you again Ricardo. To get this working, I needed to do the following.
1 - Make the procedure above its own separate *.xlam file (saved as an add-in).
2 - Add the add-in to start automatically via the developers tab
3 - Add the Macro to the Ribbon.
I have modified the sample above to included the open statement along with the statement that executes a macros from another workbook.
Appreciate the guidance.

How to Retrieve hidden sheets in an Excel add-in?

I have built an Excel add-in using VBA. I have used 2 sheets containing some data while creating this add-in. When i saved it as an add-in, both sheets became hidden while add-in still works. Now i want to make few changes in the data of those sheets and code as well. How can i see those sheets hidden in add-in? Here is an screenshot attached. Here it is clear that there are two sheets ( Sheet1,Sheet9) are present but not visible. how can i retrieve these sheets?
Select ThisWorkbook. In the Properties pane, set the IsAddIn value to False. This will make the add-in's workbook (including all its sheets) visible and you can make changes to the data.
Caveat: You won't be able to save the workbook normally; doing so will give you an error and then a prompt to save it with a new name.
What you need to do instead is this: Once you have made the changes to your data, set the IsAddIn property of the workbook back to True. That will make the sheets hidden again. You can then save your add-in project from within the VBA editor.
The file would not let the user save a xla/xlam file if ".IsAddin" is false (worksheets add-in must be hidden to be saved)
If you want to use the add-in but let the user see the hidden sheets by code instead of manually you can use the following:
Sub Showbutton()
If ThisWorkbook.IsAddin Then
ThisWorkbook.IsAddin = False
End If
End Sub
Sub Hidebutton()
If not ThisWorkbook.IsAddin Then
ThisWorkbook.IsAddin = True
End If
End Sub
An Excel Error Measage will be shown but it will work after the user presses 'oK'
UNSOLVED-Suppress an Excel Error message caused by save button before going into Workbook_BeforeSave event
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not ThisWorkbook.IsAddin Then
ThisWorkbook.IsAddin = True
End If
End Sub

EXCEL 2nd Enable Editing Button appears after already Enabling

I have an excel workbook that I download using simple HTML portal that I coded. When I download the workbook it automatically renames it as "myWorkbook (27)" or whatever number of times it has been put in the downloads folder.
I have some code that changes the Save As location of the workbook to the original location that it was downloaded from. See:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
'Check to see if another workbook with the same name is open -- close if so.
If IsOpenWorkbook("myWorkbook.xlsm", True) Then
Workbooks("myWorkbook.xlsm").Close saveChanges = False
End If
ThisWorkbook.SaveAs Filename:="\\999.9.9.99\folder\anotherFolder\myWorkbook.xlsm"
Application.DisplayAlerts = True
End Sub
This has worked fine for almost a month but recently has been performing erratically. When I first download the sheet, it has the regular "Enable Editing" prompt which I accept. After accepting, the name of the workbook changes suggesting the code as run, but then has another "Enable Editing" prompt which, if I accept, will crash Excel.
Any insights appreciated.

What am I missing in my code that doesn't "Close" the workbook (.xlsm) after saving it?

What is the proper code to input in the module/button that will specifically prompt the user to "SaveAs", "Close" the (.xlsm) file, and open the same "New" sheet again?
I want to create an excel micro-enabled file .xlsm that will have a button called, "Save&New". The module/button will SaveAs, close the current file and open the same file again. For instance, when I'm filling out an audit and using the .xlsm sheet, I want to click the "Save&New" button for it to SaveAs, close it, and open the same file again but not the one I saved. Look at it has a template; only that I want the button to SaveAs, close the file, and open a "new" fresh file automatically.
Here is what I have so far:
In this image you see that I created the button called, "Save&New".
[enter image description here][1]
When I click on the "Save&New", it will "SaveAs" the .xlsm sheet.
[enter image description here][2]
This is where I need help. After saving the sheet, I want the workbook to "Close" the file.
And open the same sheet before to work on a "New" audit workbook form (.xlsm). (Not the sheet that was saved, but the sheet open before modifying it)
This is the code that I have inside the module/button called, "Save&New":
Sub Button7_Click()
Application.Dialogs(xlDialogSaveAs).Show
Dim oWB As Excel.Workbook
For Each oWB In Application.Workbooks
If oWB.Name = "_temp.xls" Then
oWB.Close
Exit For
End If
Next
Set oWB = Nothing
Workbooks.Open ("Communications Subdivisions System Audit.xlsm")
ActiveWorkbooks.Close
End Sub
The code currently "SaveAs" and it opens a "New" sheet of the same one that was used before saving it. The problem is that it doesn't close the workbook that was saved before, although it does open the "New" sheet.
Specifically, my question is:
What am I missing in my code that doesn't "Close" the workbook (.xlsm) after saving it and still opens a "New" sheet again?
Unless there is more involved with the macro, you might be able to accomplish this entirely via a "Template" feature already built into Excel and save yourself a ton of work and trouble.
Get your "new" file the way you want it, choose "Save As", and then in the File Types section choose "Excel Template (*.xltx)". Note this will change the selected save folder, so put the save dialog back where you really want the file and save it.
Now, every time you open this file you get a new copy, and when you do a regular save it won't make changes to the original.
Seems that the _ is comparing as a wildcard not as a literal string.
I played around and for some reason using the wildcard escape key doesn't work unless you compare with Like instead of =
This should work:
Dim oWB As Excel.Workbook
For Each oWB In Application.Workbooks
If oWB.Name Like "[_]temp.xlsm" Then
oWB.Close
Exit For
End If
Next

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