mathiasb
08-16-2004, 01:17 PM
Hi all,
Here is a problem I believe is tricky - wondering if there would be ideas in the pool of infinite VBA wisdom gathered in this forum :)
Here it comes. The purpose would be to, programatically through VBA in Excel, create a new workbook (that's easy enough) that contains a "model", that is, put some values in that new workbook, and in some cells programmatically put some formulas that use these values (that's still fairly easy). At that point, that would look something like this:
Sub CreateNewModel()
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks.Add
Set WS = WB.Worksheets.Add
WS.Cells(1, 1).Value = 1
WS.Cells(2, 1).Value = 2
WS.Cells(3, 1).Value = "=A1+A2"
End Sub
Now this example is trivial, but the type of formulas I am interested in is more complex, and, as you imagine, generating the string for the formula can quickly become quite a pain. So ideally, I would like to use user defined functions, which result in a much more compact notation. In the silly example before, I would define a function "mySum" in a module, and I would like to say something like:
WS.Cells(3, 1).Value = "=mySum(A1,A2)"
The problem is that the function is defined in a module attached to the "source" workbook, so when I save the new workbook the function definition will be lost. So essentially, I am looking for a way to create a workbook, and attach programmatically some modules to it. Is this feasible? I had a quick look at some references at programming into the VBE, and this sounded pretty hardcore / ugly. Are there some easy or not-so-easy-but-still-human ways to do that?
Thanks in advance for any idea,
Here is a problem I believe is tricky - wondering if there would be ideas in the pool of infinite VBA wisdom gathered in this forum :)
Here it comes. The purpose would be to, programatically through VBA in Excel, create a new workbook (that's easy enough) that contains a "model", that is, put some values in that new workbook, and in some cells programmatically put some formulas that use these values (that's still fairly easy). At that point, that would look something like this:
Sub CreateNewModel()
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks.Add
Set WS = WB.Worksheets.Add
WS.Cells(1, 1).Value = 1
WS.Cells(2, 1).Value = 2
WS.Cells(3, 1).Value = "=A1+A2"
End Sub
Now this example is trivial, but the type of formulas I am interested in is more complex, and, as you imagine, generating the string for the formula can quickly become quite a pain. So ideally, I would like to use user defined functions, which result in a much more compact notation. In the silly example before, I would define a function "mySum" in a module, and I would like to say something like:
WS.Cells(3, 1).Value = "=mySum(A1,A2)"
The problem is that the function is defined in a module attached to the "source" workbook, so when I save the new workbook the function definition will be lost. So essentially, I am looking for a way to create a workbook, and attach programmatically some modules to it. Is this feasible? I had a quick look at some references at programming into the VBE, and this sounded pretty hardcore / ugly. Are there some easy or not-so-easy-but-still-human ways to do that?
Thanks in advance for any idea,