Countif参考不同的工作表

时间:2020-06-03 14:49:58

标签: excel vba reference range countif

您好,我需要解决我现在面临的问题,甚至Google也无法帮助我。

我想向AS2字段添加一个COUNTIF公式,其中包含来自不同工作表的源信息。

COUNTIF应该跳到工作表ee_lpk,然后从A2列向下到最后使用的行末尾。并将其与字段D中的条件进行比较。

因此,AS2将与D2AS3D3进行比较。

当我记录它表明:

ActiveCell.FormulaR1C1 = COUNTIF(ee_lkp!R[-143]C[-44]:R[217]C[-44],R[-143]C[-41])"

这是可行的,但是以防万一ee_lpk页上的数字或行每天都在变化。

任何帮助将不胜感激。

马丁

1 个答案:

答案 0 :(得分:0)

您需要使用变量来解决此问题。尝试这样的事情:

sub Answer()

Dim srcRng as Range
Dim srcLength as Long

'First find how many rows on sheet ee_lpk and store it as a variable
srcLength = Sheets("ee_lkp").UsedRange.Rows.Count

'Then use that variable to get your range
Set srcRng = Range(Cells(2,1), Cells(srcLength, 1))
'Or another viable option would be:
'Set srcRng = Range("A2:A" & srcLength)

'Then put that in your Countif formula
ActiveCell.FormulaR1C1 = _
   "=COUNTIF(ee_lkp!" & srcRng.Address(True, True, xlR1C1) & ", R[-143]C[-41])

End Sub