I have an Excel / VBA question. I've come across a circumstance where VBA is being used to save a large number of activesheets as individual workbooks. Itself not an issue. However in the workbook they are being saved from there is a module that needs to be in each of the new individual files. The save method is saving a link to the module and not the module itself.
I'm looking for method of saving the activesheets as workbooks and forcing the module to be included in each of the new files. Unfortunately I'm not allowed to show code references for this but any experience someone may have with this type of issue would be appreciated.
All the files are being save as type .xlsm and also save the format is set to Macro Enabled Workbook
The Excel Version is 2013
Related
I have a simple excel VBA that is referring multiple files and copying information across to a master before processing. While building this on my own system a workbook reference (working perfectly fine) was written as:
Workbooks("key").Sheets("Sheet1").Range("A1:X57").Copy
Here key is a .xlsx file
While using this in another system this does not work and it explicitly requires the file extension in every call.
Workbooks("key.xlsx").Sheets("Sheet1").Range("A1:X57").Copy
It would not be extremely difficult for me to make this change although I wanted to understand why this is happening and can I define an Option (guessing!) that would not require me to do so?
Why is there difference in behaviour across systems while running the same script?
Any help would be much appreciated. For me this seems like VBA having a mind of its own.
If the key.xlsx file is saved on both systems, including the file extension when referring to the Workbook objects is the safer option because of Windows hide extensions setting:
The Workbooks Collection Object
If the hide extensions setting is not in effect (meaning that
extensions are indeed displayed in Windows), you must include the xls
extension when you reference a workbook in the Workbooks collection.
For example, if you have open a workbook named Book1.xls, you must use
Workbooks("Book1.xls").Activate
rather than
Workbooks("Book1").Activate
to refer to the Book1 workbook. The second line of code above, without
the xls extension, will fail with an error 9, Subscript Out Of Range,
because there is no workbook with the name Book1. If the hide
extensions setting is effect, you can omit the xls extension and use
either of the following lines of code.
Workbooks("Book1").Activate
Workbooks("Book1.xls").Activate
These lines of code assume that you do not have open both an unsaved
workbook with the name Book1 and a saved workbook with the name
Book1.xls. With the hide extensions setting enabled (so that
extensions are hidden in Windows), the two lines of code above are
functionally equivalent. As a matter of good programming practice, you
should always include the xls extension in a workbook name. This
ensures that you reference the correct workbook regardless of the
value of the hide extensions property.
More details from cpearson.com for File Extensions And Their Implications In VBA Coding
I was just introduced to VBA. However, on saving my first excel file with VBA code, with .xlsm extension (I was advised to do so), and then reopening that saved file, I realized that all the VBA code had disappeared and the cells in which I had used the user-defined functions threw errors. Saving the workbook as .xlsx instead of .xlsm also didn't help. When I tried saving the VBA module, it showed that FUNCRES.XLAM is read-only and hence cannot be modified, and so try saving it at a different location. Doing so, saved an excel file which on opening showed a pop-up message that it's corrupt so can't be opened. Then I gave all permissions to FUNCRES.XLAM and it is NOT read-only. But the problem still prevailed. What should I do? Can someone please help me?
PS: It is Windows 10, Office 2016.
Your VBA code needs to be saved in a module within the same workbook in which you are working. This will probably be listed as VBAProject(Book1)
There will likely be other projects listed in the Project Explorer window of the VBA GUI, but your code will not get saved with those modules.
These are related to add-ins, and not to your workbook.
I have a heavily formulated workbook which will be used by staff members who will paste in potentially confidential client information. The workbook then comes up with graphics and data to summarize the imputed information
In an ideal world, the people using the program will be able to Save specific sheets as a PDF and would be able to close the program without saving changes to the worksheet but Excel always demands to save the template first, THEN will allow PDFs. This is obviously a problem as if a person uses it, paste's in a client's information and saves the result as a PDF then whoever next opens the Excel workbook will have that previous client's information showing.
SO. I either need a way to tell Excel to not require compulsory saving to allow PDF conversion or another option which will result in the same thing.
I should also note that the workbook has to allow users to paste in information so I don't imagine a Read Only will help :/
I have also attempted using a Macro-Enabled Template which still has the same problem
I imagine there's a ridiculously simple solution to this.
Thanks in advance
I have a very complex Excel workbook, with an awful lot of VBA code and macros in it. For version control purposes, I'd really like to be able to save the code as separate files to the workbook proper - to split it off into modules. Is this a thing that's remotely possible?
You should also look into Add-ins (.xlam). These allow your modules/class modules to be referenced in other workbooks. If you deploy a tool to a workgroup then realize that you need to change a macro you can update the .xlam and it gets pushed to all workbooks that reference it.
I created an Excel .xlsm file with a bunch of functionality in some VBA Modules/Classes and now I've decided to separate out the code because it will be shared across 4 different sites.
I saved the vba_code.xlsm to a shared location and created my 4 different siteXYZ.xlsm files.
In each siteXYZ.xlsm file I would go to the "Tools | References" menu in the VBA editor and add a reference to the shared vba_code.xlsm at a shared file location \share_location\fileLocation\vba_code.xlsm
At this point, I tested out the functions in siteXYZ.xlsm and everything would work fine.
However, every time I saved siteXYZ.xlsm, closed Excel and then reopened the file it would lose the reference to my vba_code.xlsm file.
How can I keep the VBA references saved with my siteXYZ.xlsm file?
After spending hours searching for an answer and trying various methods such as adding a digital signature to the vba_code.xlsm file and trying to programmatically add the reference on Workbook_open I found a forum post describing the problem:
My siteXYZ.xlsm file had no VBA code or macros defined within it so Excel refused to save the VBA Project and as a result did not save the VBA Reference to vba_code.xlsm.
The solution was simple:
Add ANY VBA code to the siteXYZ.xlsm and save it.
I just double-clicked ThisWorkbook under the VBA editor and added a function to Workbook_open that doesn't do anything:
Private Sub Workbook_open()
End Sub
The usual method of achieveing this is by saving your vba_code.xlsm as an addin (XLA or XLAM) and storing it in the shared location, then adding the addin to Excel in your 4 different sites. You can also extend this approach by using your own Addin Loader instead of Excel's. There is a working example of an Addin Loader at
http://www.decisionmodels.com/downloads.htm
Following #nvuono
You need to add some kind of module/reference to the excel file for it to save the references you have added.
Private Function addJunkModuleToGetReferencesToSave(ByRef wb As Workbook)
Set new_module = wb.VBProject.VBComponents.Add(vbext_ct_ClassModule)
new_module.name = "Junk"
new_module.CodeModule.AddFromString ""
End Function