0

I have a Formula

CRange = "B" & CStr(Offset + 1)
IRange = "C" & CStr(j)
ActiveSheet.Range(CRange).Select
'Sheets("Iteration Details").Select
value = CStr(-1 - CInt(Offset) + 3)
Value1 = CStr("R[" & value & "]C")
Dim r As Range
Set r = Range(Value1)
ActiveCell.FormulaR1C1 = _
 "=IF(ISTEXT(r),(SUM(COUNTIFS('Test Case Iteration Details'!C1,'Iteration Details'!R3C,'Test Case Iteration Details'!Range(IRange),""Pass""))),"""")"

I want to increase the value of ISTEXT which is R[-1]C to R[-2]C next time the loop runs also the value of IRANGE to C4 from C3 every time the loop runs. But the Formula is not working though it is working Fine if I hard code the variables . Can you please help me out on how to proceed?

vidhik
  • 13
  • 3

2 Answers2

1

you may be after this:

    Dim Offset As Long, j As Long
    Dim IRange As String
    Dim Value As String

    j = 3 ' initializing column C as the one to start looping from in "Test Case Iteration Details" sheet
    Sheets("Iteration Details").Select
    For Offset = 3 To 12 ' change the Offset range as per your needs
        Range("B" & CStr(Offset + 1)).Select
        Value = "R[" & CStr(-1 - Offset + 3) & "]C"
        IRange = "C" & CStr(j)
        ActiveCell.FormulaR1C1 = _
         "=IF(ISTEXT(" & Value & "),(SUM(COUNTIFS('Test Case Iteration Details'!C1,'Iteration Details'!R3C,'Test Case Iteration Details'!" & IRange & ",""Pass""))),"""")"
        j = j + 1 ' update "Test Case Iteration Details" sheet column index
    Next

should that be the case, then you really should;

  • get rid of all that Select/Activate/ActiveXXX stuff and adopt fully qualified range references

  • write the formula in one shot only

and the whole code collapses to:

With Sheets("Iteration Details")
    .Range("B4:B14").FormulaR1C1 = _
         "=IF(ISTEXT(R3C),(SUM(COUNTIFS('Test Case Iteration Details'!C1,'Iteration Details'!R3C,Offset('Test Case Iteration Details'!C2,0,row()-3),""Pass""))),"""")"
End With
DisplayName
  • 13,283
  • 2
  • 11
  • 19
0

You may want to try concatenating the variable into the formula string as shown here. For example:

ActiveCell.Formula = "=IF(ISTEXT(" & r & "), stuff if true, stuff if false)"
GothicAnatomist
  • 146
  • 4
  • 15