如何在 VBA 的 countif 公式中插入变量?
How to insert a variable in a countif formula in VBA?
我正在尝试做一个 COUNTIF
函数,使用 mcol
和 mrow
作为变量。
它完美地工作,问题出现了我每次需要将 mrow
值增加 1,但是 =COUNTIF(Analysis!RC[9]:R[8]C[9],""<=3"")
中的行范围 R[] 每次增加 9.
我正在考虑将 COUNTIF
放入另一个变量 i
并放入 for
以放入 i = i + 9
,但我不确定如何修复它。
有人可以帮忙吗?
Sub Test()
Set wb = Workbooks("Book1.xlsm")
Application.ScreenUpdating = False
LastRow0 = Sheets("Tests").Range("A" & Sheets("Tests").Rows.Count).End(xlUp).Row
mcol = 3
For mrow = 2 To LastRow0
Cells(mrow, mcol + 1) = "=COUNTIF(Analysis!RC[9]:R[8]C[9],""<=3"")"
Cells(mrow, mcol + 2) = "=COUNTIF(Analysis!RC[9]:R[8]C[9],""good"")"
Next mrow
End Sub
我不清楚你想增加多少。这不会是一个大的编程问题,所以我把它留给你。我将展示如何使用变量构建公式。
您将公式创建为字符串,因此您将使用字符串操作来构造它。
Dim s As String ; for the demo, this receives the formula string
Dim i As Integer, j As Integer ; these are the variables we will use
.... ; use them in loops, increment them as you need
s = "=COUNTIF(Analysis!RC[" & Trim(Str(i)) & "]:R[" & Trim(Str(j)) & "]C[" & Trim(Str(i)) & "],""<=3"")"
Str
函数将整数转换为字符串。 Trim
函数删除 Str
放在那里的前导 space。
希望对您有所帮助。
我正在尝试做一个 COUNTIF
函数,使用 mcol
和 mrow
作为变量。
它完美地工作,问题出现了我每次需要将 mrow
值增加 1,但是 =COUNTIF(Analysis!RC[9]:R[8]C[9],""<=3"")
中的行范围 R[] 每次增加 9.
我正在考虑将 COUNTIF
放入另一个变量 i
并放入 for
以放入 i = i + 9
,但我不确定如何修复它。
有人可以帮忙吗?
Sub Test()
Set wb = Workbooks("Book1.xlsm")
Application.ScreenUpdating = False
LastRow0 = Sheets("Tests").Range("A" & Sheets("Tests").Rows.Count).End(xlUp).Row
mcol = 3
For mrow = 2 To LastRow0
Cells(mrow, mcol + 1) = "=COUNTIF(Analysis!RC[9]:R[8]C[9],""<=3"")"
Cells(mrow, mcol + 2) = "=COUNTIF(Analysis!RC[9]:R[8]C[9],""good"")"
Next mrow
End Sub
我不清楚你想增加多少。这不会是一个大的编程问题,所以我把它留给你。我将展示如何使用变量构建公式。
您将公式创建为字符串,因此您将使用字符串操作来构造它。
Dim s As String ; for the demo, this receives the formula string
Dim i As Integer, j As Integer ; these are the variables we will use
.... ; use them in loops, increment them as you need
s = "=COUNTIF(Analysis!RC[" & Trim(Str(i)) & "]:R[" & Trim(Str(j)) & "]C[" & Trim(Str(i)) & "],""<=3"")"
Str
函数将整数转换为字符串。 Trim
函数删除 Str
放在那里的前导 space。
希望对您有所帮助。