0

I've been working on an Excel macro to calculate performance metrics, based on data obtained from TFS integration. I've come to a point where I'm trying to count ocurrences of some events. Based on my parameters, sometimes it's as easy as count when some specific text is found, but sometimes I'd have to count when the cells contains some text string.

So far, I know I could use the COUNTIFS formula by building it on code and then assign this to any cell. I'm getting error when, while building the formula string, I try to get the string that contains the text. I have a condition to assign this value, as follows:

If rowNumber = x Then
    myString = "Found text"
ElseIf rowNumber = y Then
    myString = "*contained*"
End If

formula = "=SUM(COUNTIFS(Range1, Condition1, Range2, " & myString
If moreParameters Then
    formula = formula & ",Range3, Condition3))"
Else
    formula = formula & "))"
End If

Cells(i,j).Select
ActiveCell.FormulaR1C1 = formula

So, when the specific text is found (case rowNumber = x), it works fine, but when I try to use the wildcard, it doesn't understand the quotes as part of the string. I tried with double quotes:

ElseIf rowNumber = y Then
    myString = ""*contained*""
End If

But then it throws compile error (Expected: end of statement), and with CHR(34) as well, without success. How come I could include the expression to use the wildcards in the formula string to use later?

Community
  • 1
  • 1
Juan Se
  • 15
  • 3

0 Answers0