file forgetting dependent reference - excel

I have a spreadsheet with code called 'constants.xlsm', which other .xlsm spreadsheets use as a dependent reference.
I have a new spreadsheet file where I want to use a function from 'constants.xlsm' in a worksheet formula. The function is =ToOptionSymbol(...).
The function works as expected while the new file has a reference to 'constants', but does not want to remember that it has a reference to 'constants' when it is closed and reopened.
The 'new' file has no code modules of it's own. How can I make the 'new' file remember the reference between sessions?

It seems that in addition to saving the workbook as .xlsm, it also needs to contain a VbaProject, to which the VbReferences to other files are added.
Try the any of these:
Add a new module to the new workbook.
If you don't want to have an empty module in the "new file" you can also add a dummy procedure into the ThisWorkbook code pane, such as this:
Private Sub Dummy_Procedure()
End Sub
Any of the above suggestions will force the workbook to create a VbaProject, thus keeping the vbReference to the constants.xlsm vbProject.

Related

Execution of worksheet subroutine upon creation of workbook from a template

I would like to know, please, if it is possible to write a function which would be contained within a workbook template, specifically within a particular worksheet that would be executed upon creation of that workbook via the Workbooks.Add("template.xlsm") method. Just as importantly the routine would only be executed upon creation.
In addition I would need to pass a set of parameters to the said funtion when performing the Add. Is that possible?
If not, it appears that I would have to either call the function (Application.Run) immediately after adding the workbook (now part of another project) or manipulate the contents of the new sheet directly which would not be as elegant IMHO.
Any help or suggestions would be gratefully received
If the function will only be executed at the time of creation, then why does it need to be contained within the new workbook?
What's wrong with simply doing the following:
Sub New_Workbook
Dim wb As Workbook
Set wb = Workbooks.Add("template.xlsm")
' Execute your code here. It will only be called on creation
End Sub
Perhaps your question wasn't clear as to why the code needs to be contained within the new workbook if it's never going to be run again. If you do need to copy code into a module of the new workbook, then you can use VBA Extensibility; or alternatively add a reference to the new workbook to a code add-in.

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.

VBA Macro for custom Excel 2010 function works in one workbook, causes invalid name error in another

I'm using this VBA macro in an Excel 2010 workbook to define the function FileSize, which enables me to pull the size of a document into a document master worksheet using the filepath.
Function FileSize(FileName As String)
FileSize = FileLen(FileName)
End Function
I then use the FileSize function to reference a file path string in column A like so:
=FileSize(A1)
This works in the workbook I wrote it for initially, but when I copypaste the macro for the Function into a new module for a new worksheet, I get an invalid name error.
Both workbooks are macro-enabled (.xlsm), and activating or deactivating option explicit hasn't had any effect. What am I doing wrong?/What am I neglecting to do?
#NAME suggests that your new workbook has no idea what the formula =FileSize() is referring to.
Double check to insure that you are actually putting that function in a module in the same workbook in which you are using the formula.
Make sure that you have defined the code as a Function and not a Sub.
Rip your computer off your desk and throw it out the window.
Thank you, JNevill! Luckily, defenestration won't be necessary.
I figured it out--I gave my module a name that was identical to the function it contained. Just changed the module name, and now the functions populate correctly.

NOW() in Excel 2010

I am just wondering if there is an easy way to make the function "now()", in Excel, just to work when a modification is done on the workbook, and not when the workbook is opened.
take a look at this:
http://www.extendoffice.com/documents/excel/954-excel-created-last-modified-time.html#three
and try using the second macro from:
Sub Workbook_Open()
Range("A2").Value = Format(ThisWorkbook.BuiltinDocumentProperties("Last Save Time"), "short date")
End Sub
Replace A2 with the cell you want to put that last modified time in.
you can also add:
Public Function ModDate()
ModDate = Format(FileDateTime(ThisWorkbook.FullName), "m/d/yy h:n ampm")
End Function
and then call:
=ModDate()
EDIT:
You can also use:
FileDateTime()
http://www.techonthenet.com/excel/formulas/filedatetime.php
You'd have to figure out how to get the file to refer to itself.
Bear in mind though, that a file is changed by being opened, so it might as well be the current time. For more on this, see:
http://support.microsoft.com/kb/826741
So NOW() might be all you have. Also, you can search Excel's components in program files, but if NOW is written in its binary, it might not be changeable.
However...
According to microsoft,
Making your custom functions available anywhere
To use a custom function, the workbook containing the module in which
you create the function must be open. If that workbook is not open,
you get a #NAME? error when you try to use the function. Even if the
workbook is open, if you use the function in a different workbook, you
must precede the function name with the name of the workbook in which
the function resides. For example, if you create a function called
Discount in a workbook called Personal.xls, and you call that function
from another workbook, you must write =personal.xls!Discount(), not
simply =Discount().
You can save yourself some keystrokes (and possible typing errors) by
selecting your custom functions from the Insert Function dialog box.
(Your custom functions appear in the User Defined category.) An easier
way to make your custom functions available at all times is to store
them in a separate workbook and then save that workbook as an add-in
(an XLA file) in your XLStart folder. (The XLStart folder is a
subfolder of the folder containing your Excel files. When you start
Excel, the program opens any documents it finds in XLStart.) To save a
workbook as an add-in, choose File, Save As (or File, Save). Then
choose Microsoft Excel Add-in from the Files Of Type list.
If your user-defined functions are stored in an XLA file that is
present in memory, you don't have to specify the name of that file
when you call a function. If the XLA file is saved in your XLStart
folder, it will be present in memory whenever you run Excel.
http://office.microsoft.com/en-us/excel-help/creating-custom-functions-HA001111701.aspx
So it seems implied that, provided user defined functions are in that workbook, they should work just fine wherever the file is.
Hope that gives you your answer.

Excel VBA - Use Module in Workbook B to Update Data in Workbook A

I have 10 XLS's, each of which contain a a few hundred lines of VBA that do the same thing. I want to move this common code into an 11th XLS, and have the other 10 call the code in the 11th XLS. The common code must have access to all of the data and worksheets in the calling XLS. This last requirement does not seem to be addressed by other answers to this question on SO. Can I pass the calling XLS's worksheets in as a parameter, or something similar?
Instead of putting this into a secondary XLS file, I'd recommend creating an XLA file (an Excel Add In).
This is the exact scenario for which XLA was intended. XLA will work the way you intend in this case.
For details on creating an XLA, see this page.
Yes, you can pass references to workbooks, worksheets, ranges, etc. as parameters to any function:
Public Sub CallMe(ByVal oWorkbook as Workbook)
Dim oWorksheet as Worksheet
Set oWorksheet = oWorkbook.Worksheets(1)
' Do stuff...
End Sub
Note that you'll probably have to re-write a lot of the code you copy from the 10 workbooks since they'll be full of implicit references to "this" workbook, such as Worksheets(1) etc. As in the example above, you now need to say oWorkbook.Workbooks(1) instead.

Resources