How to check if VBA module has been modified? - vba

I have an Excel workbook that I would like to export the code for every time the workbook is closed but I would like to do so selectively so that each module will only get exported if it has been modified since the workbook was opened. I would like to be able to use the exported files to run a diff on the code and I'm assuming that re-exporting every time will cause the files to always be flagged as different. Here's an attempt at some pseudo-code to show what I mean:
for each module in Workbook
if module.isModified() then
module.export
end if
next module

VBComponent has a readonly property. Add reference to VBE and you should be able to access "changed" VBComponents
bool Saved
Saved Property - (Excel 2003 VBA Programmer's Reference Google Books)

I was just being lazy about it and hoping someone already new a method that I hadn't found. Sorry about that.
I found that I can use the following.
for each module in Workbook
if not module.saved then
module.export
end if
next module
Then, I'll just run the export function every time the workbook is saved.

Related

VBS GetObject will not bind to saved workbooks

I'm trying to write a script which will switch to a specific excel workbook and enter keys, however, the script will only run if the workbook which is being referenced is unsaved.
For example:
If I open a new excel workbook which creates a new workbook named "Book1"; the following works:
Set xl = GetObject("Book1").Application
If I run the above when the workbook has been saved, even if the name remains the same, the above results in the following error: (0x800401E4).
Is anyone aware of this being a problem, or can anyone point out what I'm doing wrong? I'd guess the error has something to do with the object name being different from the actual file name, so when excel creates the default workbook the code works because the name of the instance is "Book1" whereas the saved "Book1" is assigned another object name when saved?
I wasn't sure if I needed to close this question as answered, however, it seems that https://stackoverflow.com/users/4088852/comintern has resolved the issue.
It needs to be the full path -
GetObject("C:\Users\Me\Documents\Book1.xlsm").
Thanks,

Where to save excel vba function to be able to access the function in other workbooks

I've created a function and I would like to be able to open any excel file and use this function just by typing into a cell '=function'. Is this possible and how do I do this? Where do I save the function?
Save the workbook containing the function as an add-in (either .xlam or .xla depending on your Excel version). You can then install it via the Add-in manager and call it from any workbook.
Note: you don't technically have to save it as an add-in - you can use a regular workbook - but then you will have to prefix the function name with the name of the workbook whenever you call it (e.g. =Personal.xlsb!some_function), and you will have to remember to open the workbook each time (or put it in your XLSTART or other startup folder).
I'm going to provide an answer with the example of Workbook A as wbCompany and Workbook B as wbEmployee.
From what I understood is that you have a function in wbCompany.getEmployeeCount() and you want to use this function in wbEmployee.
Firstly, rename the VBA Projects of both files to prevent duplicate project name. So we will rename the VBA Projects as vbaPrjEmp and vbaPrjCmp for wbEmployee and wbCompany workbooks respectively.
Secondly, you need to add wbCompany as reference to wbEmployee.
In wbEmployee, open the Microsoft Visual Basic for Applications window.
Select Tools > References.
In the References dialog that appears, click on Browse.
In the Add Reference dialog that appears, select Microsoft Excel Files from the Files of type box, select the file that you want to call (in this case wbCompany), and click on Open.
Choose OK to close the References dialog.
Finally in wbEmployee you can now refer/call the function from wbCompany in the following manner:
Sub compareEmpCount()
msgbox vbaPrjCmp.ThisWorkbook.getEmployeeCount
End Sub
You can store it either in your Personal.xlsb workbook or in an Excel add-in (.xlam). Search for either of these two to get you on the right track.
Well, first of all, you have to enable macros in Excel.
After that, you can open Excel VBA editor with Alt+F11, and create your function there.
Finally, you can use your function in a cell with '=function'.
Anyway, you can try this link to help you with your first vba function: http://www.fontstuff.com/vba/vbatut01.htm
Note: I've already created some functions on Excel, and it always worked for me. But always remember: you must enable macros in Excel.
You can read about macros here: https://support.office.com/en-nz/article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12

A private subroutine in VBA is being activated by other workbooks -- Why?

I have a private subroutine in Workbook A that is running any time I open or close and save other unrelated workbooks. I'm trying to understand why that occurs so I can capture all potential errors that may occur.
The subroutine is an ActiveX ComboBox named TabProg that is supposed to run when the value is changed. I have currently added in a check to see if the active sheet trying to run the code is the "Program Loading" sheet to try and divert any potential errors. See snippet below.
Private Sub TabProg_Change()
MsgBox "Whomp!", vbOKOnly + vbExclamation
If ActiveSheet.Name <> "Program Loading" Then 'do nothing
Else
'Run desired actions on "Program Loading" sheet
End If
End Sub
Any known reasons why this is occurring or other ways to catch it would be helpful. Thanks!
Edit 1: I do not see this problem occur when I open other workbooks in new instances of Excel.
Edit 2: I have modified the code to include a message box whenever the code tries to run, now shown above. It is occurring every time I open or close and save any Excel file, including the file itself. The drop down list in the ActiveX ComboBox is a list of names that correspond to 10 sheets within Workbook A. If I delete the sheet that ComboBox is currently set to, the error will disappear. If I change the ComboBox to a different sheet, the error will reappear.
From what you've wrote in your question, I don't think that your problem can be replicated. I think that your Excel file got corrupted. I had an experience like this: had a file for experimenting with macros, one of the macros used Excel Speech object to say "This is a test file" on opening that particular file. The macro was (as all other macros from this file) not part of my PERSONAL workbook, it was assigned to ThisWorkbook in the bespoke file. At some point a funny thing happened: this "This is a test file" private subroutine got activated every time I opened any Excel file. I did not find any solutions, I just deleted the file where the subroutine was stored. This resolved the problem, but I have no explanation for this. I am afraid the same thing may apply to your file, but maybe other folks have a better idea... maybe it's something in the system registry??? I don't know. Can you manually copy elements / code from this file to a newly created file and just delete the original?

Copy Method of Worksheet class fails with .xlam file

CONTEXT: I have created an Excel Add-In in which there is a worksheet named "Mother". When the user clicks a button from any workbook in which the Add-In is installed, I would like the worksheet "Mother" to be copied inside its same workbook and being renamed. In code:
ThisWorkbook.Sheets("Mother").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
WHAT I EXPECT: being the line of code above placed into the .xlam code, I know that ThisWorkbook refers to the Add-In and this is observable through a watcher as well:
So, I expect that a copy of "Mother" will be added to the workbook, at the end.
WHAT HAPPENS: the method simply fails, returning the error Copy Method of Worksheet class failed.
WHAT I HAVE TRIED: By simply looking for the name of the error online I have figured out that, apparently, I cannot copy a hidden worksheet and that's why the method fails. So I have tried:
1) To change the visibility with ThisWorkbook.Sheets("Mother").Visible = True... No success;
2) To activate the workbook before/after to change the visibility, so ThisWorkbook.Activate... No success.
I'm not being able to find much documentation online about this, so I'm almost wondering if it's actually possible to edit/show the workbook.xlam, something that I would really need to do for my project.
A BIT MORE OF CONTEXT: The reason why I need to both add the worksheet and show it to the user is that the Add-In, which basically consists in a custom function =myFunction(), needs to be fed by user-inputs for make its calculus.
In particular, neither I can write the data in the sheet before to distribute the Add-In (because I don't know the user inputs in advance), nor I can prepare a user-form where to insert the data and show it at running-time (because the user-inputs are usually a big amount of data that can be copied-pasted easily into an Excel spreadsheet, but not inserted manually into a form nor in the function parameters themselves). So:
MY QUESTION: Is it possible to copy (duplicate) a worksheet within a .xlma workbook and activate it after to allow the user data insertion? If yes, where am I being wrong on the above?
Looks like this link would help: http://forums.techguy.org/business-applications/1120165-add-extra-sheet-xlam-file.html.
The operation cannot be performed on add-in workbook, hence you just need to set it as "not add in":
ThisWorkbook.IsAddIn = False
'your copy operation
This Workbook.IsAddIn = True

Where Exactly To Store VBA

We receive Excel files daily from our field offices which I have to clean and re-format (about 110 columns and 500 rows-worth) using VBA.
I need to save my VBA as a macro so we can use it to clean up all the workbook we receive by running the macro and saving the edited sheet as a new worksheet by getting the name from UserForm Combobox items.
Where exactly should I store the VBA snippets? I mean when I open the Visual Basic panel, I have these three options:
Running The Code From Microsoft Excel Object :Sheets1(Sheet1)
Running the Code From An Inserted Module
Running the Code From User Form
If I am supposed to use options 1 or 2, how can I call the UserForm for saving the sheet?
I Recomend you to use modules (Option B)
Option C goes with option B, ill explain, you can create a sub in a module in option B, then you can do:
UserForm1.show
In Option B I would writte this code, but before trying this i recomend you to understand a bit more of vba
sub ClearWBs()
'opening workbook
Workbooks.Open Filename:="c:\book1.xls"
'your code
'your code
'below code for saving and closing the workbook
Workbooks("book1.xls").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
end sub
Use Module:
If your VBA code focusses on data summarization and manipulation I suggest you use a Module.(example is what you have described in your question).
Use Form:
If what you wan't to do requires a GUI(Graphical User Interface) then you'll have to resort to Form where you can design your GUI. (example is if you have many fields that the user needs to fill-up with distinct values in which you provide the choices)
Use Excel Object:
If what you wan't to do has something to do with events happening on Worksheet and/or Workbook, like for example whenever you add sheet the macro runs, or when you open or close the workbook the macro runs then you will have to write the macro in the Excel Object.
This is basically what i have in mind, hope this helps.
If you receive files that do not contain VBA and you need to apply the same code on those files all the time then I propose that you either save that code in your personal workbook.
You can see how to do that here: http://office.microsoft.com/en-ca/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx
This is nice because you can also tie it to keyboard shortcut or just have it always ready for you to use.
The disadvantage is that it will only be set up per user session per computer. What you can do is have that code all set up in a module and then import it into your personal workbook if you change session or if someone else has to do it.
Once it's done, you will not have to include the module in your files your receive again.