我试图通过在前2列中查找并将它们与同一Wokbook中的另一个表进行比较来使用countifs语句。引用RrC1,RC1或其他任何内容均无效。结果我只能得到“ 0”。如果我输入常量,它将起作用。我确定我的论证2、4、6是问题所在。我只是想不通为什么!
Sub DataBase()
'Set my tables
Dim Answers As ListObject
Dim Table As ListObject
Set Answers = Worksheets("quantitativ").ListObjects("DataQuant")
Set Table = Worksheets("Database").ListObjects("Tabelle7")
'Set my Ranges for filters (Organizational level, Location, Function...)
Set OrgRange = Answers.ListColumns(1).Range
Set LocRange = Answers.ListColumns(2).Range
'Set Ranges for Answers to Questions (Scale)
Set Q1 = Answers.ListColumns(5).Range
Dim r As Long 'Row variables for For-Loop
For r = 5 To Table.DataBodyRange.Rows.Count + 4
'Q1
Cells(r, 6).FormulaR1C1 = _
Application.WorksheetFunction.CountIfs(Q1, RrC5, OrgRange, RrC1, LocRange, RrC2)
Next r
End Sub
感谢您的帮助!
答案 0 :(得分:1)
Cells(r, 6).FormulaR1C1 = _
Application.WorksheetFunction.CountIfs(Q1, RrC5, OrgRange, RrC1, LocRange, RrC2)
这真是一团糟。您正在尝试使用工作表函数的 结果 加载公式。
如果您要将公式加载到单元格中,则可以这样做:
Cells(r, 6).Formula = "=CountIfs(" & Q1.Address & ", " & _
Cells(r, 5).Address & ", " & OrgRange.Address & ", " & _
Cells(r, 1).Address & ", " & LocRange.Address & ", " & _
Cells(r, 2).Address & ")"
甚至:
Cells(r, 6).Formula = .Formula = "=CountIfs(" & _
Q1.Address & ", E" & r & ", " & _
OrgRange.Address & ", A" & r & ", " & _
LocRange.Address & ", B" & r & ")"
但是,如果要对公式求值并将结果仅转储到单元格中。.
Cells(r, 6).Value = Application.WorksheetFunction.CountIfs(Q1, _
Cells(R, 5), OrgRange, Cells(R, 1), LocRange, Cells(R, 2))
请记住,尽管使用所有这些选项,Cells(..
并不完全合格。
将所有内容更改为.Cells(..
会更好,将很多内容打包
With WorkSheet("DESTINATION_SHEET")
...
...
End With
强烈建议。