VBScript tips - excel macro to fill formula
Thanks for the post here, then I can now use the syntax highlight in google post.
As a programmer, takes several hours to figure out some vb usage. Some performances/concepts are different as normal programming language. So I summary some tips as below:
The example snippet I implemented is as below, this macro will generate excel formula and fill them to the excel file.
1. Better to use "Variant" type for array type if the array size is unknown yet. If want to define an array of string, then need to give the size of the array. The array is start with index 0.
2. Can not use variable in "Dim" definition, need use ReDim after a Dim.
3. Use MsgBox() to get the info to test value of variables.
4. Use "&"(without ") to concatenate or join strings and variables.
5. Can use "ThisWorkbook.Sheets", then the macro must be in the same excel file, or use "ActiveWorkbook.Sheets", then can use macro from other opened .xlsm in current/active .xlsm or .xlsx file.
6. When use excel formula with string in it, then need use a double-quote(") to escape the double-quote(").
7. An "INDIRECT()" function must be use when need to return the result of the CELL() function as part of the formula.
8. Display number 0 in excel or not(be empty when result is 0). Number format with thousand separator "#,##0" will let the excel display 0; "#,###" means not display 0.
9. Always add a MsgBox() after the script is done. This is a good habit to notify users the process is completed/finished. This is especially help for non-tech users.
Sub FillReportSummary() ' ' FillReportSummary Macro ' ' Dim description As Variant description = Array("Type A", "Type B", "Type C", "Type D") Dim headerNum As Integer headerNum = UBound(description) + 1 'MsgBox ("headerNum: " & headerNum & description(0)) Dim headerFormulas() As Variant ReDim headerFormulas(1 To headerNum) Dim strFormulas() As Variant ReDim strFormulas(1 To headerNum) Dim calSumFormula As String 'With ThisWorkbook.Sheets("SheetName") With ActiveWorkbook.Sheets("SheetName") For i = 1 To headerNum headerFormulas(i) = "=IF($A5=""Type"", ""Total" & description(i - 1) & ""","""")" calSumFormula = "SUMIF(INDIRECT(CELL(""address"",INDEX($B$1:$B14,MATCH($B14,$B$1:$B14,0)))):$B14,""=" & description(i - 1) & """,INDIRECT(CELL(""address"",INDEX($B$1:C14,MATCH($B14,$B$1:$B14,0),2))):$C14)" strFormulas(i) = "=IF($A14=""Total Client"",IF(ISBLANK(" & calSumFormula & "),""0"", " & calSumFormula & "),"""")" 'MsgBox (headerFormulas(i)) 'MsgBox (strFormulas(i)) Next i .Range("G5:J5").Formula = headerFormulas .Range("G14:J14").Formula = strFormulas .Range("G14:J4000").FillDown .Range("G14:J4000").Font.Color = vbRed .Range("G14:J4000").Font.Bold = True .Range("G14:J4000").NumberFormat = "#,##0" End With MsgBox ("Macro run to get the client level summary is completed!") End Sub
1. Better to use "Variant" type for array type if the array size is unknown yet. If want to define an array of string, then need to give the size of the array. The array is start with index 0.
Dim description(4) As String description = Array("Type A", "Type B", "Type C", "Type D")
2. Can not use variable in "Dim" definition, need use ReDim after a Dim.
3. Use MsgBox() to get the info to test value of variables.
4. Use "&"(without ") to concatenate or join strings and variables.
5. Can use "ThisWorkbook.Sheets", then the macro must be in the same excel file, or use "ActiveWorkbook.Sheets", then can use macro from other opened .xlsm in current/active .xlsm or .xlsx file.
6. When use excel formula with string in it, then need use a double-quote(") to escape the double-quote(").
7. An "INDIRECT()" function must be use when need to return the result of the CELL() function as part of the formula.
8. Display number 0 in excel or not(be empty when result is 0). Number format with thousand separator "#,##0" will let the excel display 0; "#,###" means not display 0.
9. Always add a MsgBox() after the script is done. This is a good habit to notify users the process is completed/finished. This is especially help for non-tech users.
Comments
Post a Comment