Storing the set variable value of one macro inside a set variable outside the macro in which it is defined - apache

I have the following macro that sets a variable with a string .How can i use this value inside another macro's set variable?Where somename="Outpatient"
#macro(deepak $somename)
#set($somename1=$somename)
#end

If this macro is called at any point in your template, before the second macro wants to use that variable, it should still be available. So simply
#set $var = $somename1
should work. Or simply use directly $somename1 of course.
From velocity user guide:
Once a value has been assigned to a variable, you can reference the variable anywhere in your HTML document.

Related

How can I append a constant to a variable in an Install4j text field?

I am using the "Download File" action in Install4j, and I need to use a variable in the text field where you specify the URL from which to download the file. I know that I can use a variable like this: ${installer:codebase}. However, I need to append the file name onto the end of that variable. Does anyone know how to do that?
I have tried ${installer:codebase}filename.jpg and ${installer:codebase}+filename.jpg, but neither seem to be working.
Installer variables are simply replaced in text properties. For example, if you set it to
${installer:codebase}/filename.jpg
and the value of "codebase" is set to "http://mycorp.com", then the text after replacement will be
http://mycorp.com/filename.jpg

Proper procedure for declaring global variables in VBA?

I have a macro workbook with a number of worksheets that exist permanently, which are constantly cleared, updated, etc. Since they are referred to in various subroutines, I have made each corresponding worksheet object a pseudo-global variable in the following manner, for example for the "Main" sheet:
Function MAIN() As Worksheet
Set MAIN = ThisWorkbook.Sheets("Main")
End Function
By doing so, I can then refer to each sheet in the other subroutines, for example:
MAIN.Cells.ClearContents
I have also defined some pseudo-global constants which are located in a fixed place on the "Main" sheet in a similar way, for example:
Function NumLines() As Integer
NumLines = MAIN.Range("C3").Value
End Function
In this way, I use "NumLines" just like any variable throughout the code.
I expect that there is a more efficient way to manage globally accessed variables like these and was wondering, what would be a better way to accomplish this?
For reliable sheet reference I would suggest to use Sheet.CodeName Property. Each sheet has its unique CodeName which you could find in the place marked yellow on the picture below.
For quick reference to cell value I would suggest to use Range name. After you select you C3 cell you need to put unique name in the box marked yellow below. All Range names are unique in the workbook.
As a result you can use sheet and cell reference as presented below in each of your subroutines in your project.
Sub Test_Macro()
Debug.Print MAIN.Name '>> result: Sheet1
Debug.Print Range("CellC3").Value '>> result: 100
End Sub
I expect that there is a more efficient way to manage globally accessed variables like these and was wondering, what would be a better way to accomplish this?
When I use global variables in VBA, I do three things.
I always preface global variables with a g_ prefix. It seems often that a global variable in VBA is useful. But I've also spent far too long trying to track down "what variables are global or not?" in other people's code. Keeping a very clear naming convention will save you and whoever looks at your code a TON of hassle in the future.
This is even more important if you are less experienced as a developer. Avoiding globals is hard in VBA, and the less experience you have, the more likely it is you will use globals. For others to help or maintain the code this becomes so important.
If you are going to be using even a small number of global variables, you must use Option Explicit unless you want to cause nightmares in maintaining code. It's hard enough to track down these errors when you wrote code let alone months or years later.
I always create a module which is called "GlobalVariables" or something similar. That module contains all of the global declarations in one location. For larger code bases this can become longer but it has always paid off for me because I know exactly where all my globals are defined. None of the "which file is this variable actually being defined in?" game.
Just an unrelated note, too, in your first example - I would use the code name rather than that function. Each VBA worksheet has a sheet name ("Main" in your case) as well as a codename, which you can set in VBA and remains the same. This prevents users from changing the name of "Main" and breaking code.
You can also refer directly to them similar to how you are using MAIN.Cells. KazJaw has a good example of this.

Excel VBA: Initialise public module-level variables when file is opened?

I have a whole bunch of dates in three columns which are used by several macros. Currently each macro that uses these dates has to:
Declare the range
Have a for-loop through the range to get the date-cell and
Read the value of the cell into a variable of type date
This also includes checking the cell isn't empty, checking the date is valid.
Instead of this, I would like to have a macro that reads these dates into (VBA) arrays, which persist as long as the file is opened. I would also like to have this macro run when the file is opened, so that the dates are guaranteed to be initialised when any macro that uses them is run.
Any way to do these things?
Put your code in the Workbook_Open() event.
Alt-F11 to bring up the VBA Editor
Double-click the "ThisWorkbook" in the Project Explorer.
You'll see two drop-downs at the top of the code window. Pick "Workbook" from the first one and "Open" from the second to have a stub for your code created.

VBA Goalseek with Variable instead of Cell

I need to use Goalseek in my VBA code, but rather than seeking a goal for a target "CELL" in my spreadsheet, I need to seek a goal for a "VARIABLE" within my VBA code.
e.g.
Rather than setting some target CELL to "A3", I need to set a target VARIABLE declared in my VBA code.
Same applies, of course, to the "By changing" field.
Is that even possible?
Use a cell location that is off-screen to do the calculation, then once the process is finished, load the cell value into your variable, e.g.:
...GoalSeek ... ChangingCell:=Worksheets("Sheet1").Cells("A5000")
MyVar = Worksheets("Sheet1").Cells("A5000").Value

Excel VBA - Programmatically change chart's source?

I've got this Excel document which contains several charts which target Named Ranges that are all predefined. The problem is, the reference the chart needs have to include the name of the document for some reason. I've managed to add the filename of the document in the sheet. But I cannot target this cell directly when assigning the source of the chart.
So, when I edit the chart and select the source, it looks like this:
='MyExcelDocument.xlsm'!Graphs_TotCost
For some reason Excel doesn't understand when I target the cell that contains the filename. So I'll probably have to do this via a macro by first defining a string variable, assigning the document name to that string. And then assign the completed string combined with Graphs_TotCost. So, how do I select a chart that's in Sheet3 and change the source of that chart using a macro?
You can change the source of a graph with this kind of statement:
Charts("Chart1").ChartWizard Source:="Graphs_TotCost"
You could also probably use the SetSourceData method of the Chart object.