Vba 在另一个工作表中计数多个列

Vba 在另一个工作表中计数多个列,vba,excel,Vba,Excel,如果工作表中H列或R列中的单元格为“1”,我希望我的子单元格计数。在“9000谷歌搜索”之后,我希望你能帮助我正确地做这件事 Dim studyboard1 As Long studyboard1 = Application.WorksheetFunction.CountIf(Sheets("Delayed Students").Range("H:H, R:R"), 1) 我也试过了 studyboard121 = WorksheetFunction.CountIf(Worksheets("D

如果工作表中H列或R列中的单元格为“1”,我希望我的子单元格计数。在“9000谷歌搜索”之后,我希望你能帮助我正确地做这件事

Dim studyboard1 As Long
studyboard1 = Application.WorksheetFunction.CountIf(Sheets("Delayed Students").Range("H:H, R:R"), 1)
我也试过了

studyboard121 = WorksheetFunction.CountIf(Worksheets("Delayed Students").Columns("H,R"), "121")

您需要分别计算每一项,然后在两者同时为时进行减法:

Dim HCount As Long
HCount = Application.WorksheetFunction.CountIf(Sheets("Delayed Students").Range("H:H"), 1)

Dim RCount As Long
RCount = Application.WorksheetFunction.CountIf(Sheets("Delayed Students").Range("R:R"), 1)

Dim BothCount As Long
'If you want to remove the duplicate count when R and H are 1 then uncomment the following line
'BothCount = Application.WorksheetFunction.CountIfs(Sheets("Delayed Students").Range("R:R"), 1, Sheets("Delayed Students").Range("H:H"), 1)

Dim studyboard1 As Long
studyboard1 = HCount + RCount - BothCount

什么-你为什么要做一个“Bothcount”和减法?你能不能只做两次计数,然后把它们加在一起?@Simon这取决于你的数据,如果你在H中有1次,在R中有1次,它会对那一行进行两次计数,如果这是你想要的,那么是的,删除这两次计数。如果您希望只计算一次,那么将其保留,因为当该行的R中有1个,H中有1个时,它将从2的计数中删除1。R行和H行包含“相同类型的数据”,因为某种原因,它刚刚移到另一行-因此我需要同时计算这两行。。但是谢谢你的解释!:)然后只需删除BothCount引用,就可以得到答案。