[英]Iterate though each Column in a Formula in VBA Excel
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. 我想在下一次循环运行时将ISTEXT的值从R [-1] C增加到R [-2] C,并且每次循环运行时IRANGE的值也从C3增加到C4。 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?
您能帮我解决一下吗?
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 摆脱所有“
Select
/ Activate
/ ActiveXXX
内容,并采用完全合格的范围引用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.