Excel Macro Help (Visual Basic) How to sum a column with variables

austin316

Diamond Member
Dec 1, 2001
3,572
0
0
I'm making an excel macro to total the columns, and I can't figure out how to input variables as part of the expression. I currently have two strings, which have the following values stored in them:

string1 = H2
string2 = H21

I need to know the code to sum that range. I'm guessing the code should be something like:

Range("H22").Formula = "=SUM(string1:string2)"

but I'm not sure where the quotes are supposed t go and if I'm supposed to use something instead of .formula, like maybe .value.

Thanks in advance.
 

WW

Golden Member
Jun 21, 2001
1,514
0
0
ok, you want the vba code to insert the formula in H22...

do this:

string1 = "H2"
string2 = "H21"
Range("H22").Formula = "=SUM(" & string1 & ":" & string2 & ")"

the ampersand & is concatenate...so just add up the components you want in the formula string...working around the variables, using quotes to enclose the text parts.

WW