Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ActiveCell.Formula = Variable Not Working

I am trying to fill the active cell with a formula. An example of the desired formula is =SUMIF(K30:K40,DescVar,E30:E40) where DescVar is string variable.

Here is the snip of code that is giving me problems:

If Selection.Value <> DescVar Then

Selection.Offset(1, -6).Select

Dim CalcRow As Integer, FromRow As Integer, ToRow As Integer
CalcRow = ActiveCell.Row
FromRow = CalcRow + 1
ToRow = FromRow + 10

Dim SumFormula As String
SumFormula = "='SUMIF(K" & FromRow & ":K" & ToRow & "," & DescVar & ",E" & FromRow & ":E" & ToRow & ")"

ActiveCell.Formula = SumFormula

End If

Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since the end result needs to be something like =SUMIF(K1:K10,"foo",E1:E10), you need to add the quotes around DescVar, and they need to be doubled up:

SumFormula = "=SUMIF(K" & FromRow & ":K" & ToRow & ",""" & DescVar & """,E" & FromRow & ":E" & ToRow & ")"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading