简体   繁体   中英

Count if across multiple worksheets excel vba

I am getting object defined error while running the below code.Please help me debug the code.

For i = 5 To 11
Cells(i, G).Value = Application.WorksheetFunction.CountIf(Workbooks(1).Sheets(1).Range("H:H"), Cells(i, F).Value) + Application.WorksheetFunction.CountIf(Workbooks(1).Sheets(2).Range("H:H"), Cells(i, F).Value)
Next i

You need quotes around your columns, otherwise F and G are variables.

For i = 5 To 11
Cells(i, "G").Value = Application.WorksheetFunction.CountIf(Workbooks(1).Sheets(1).Range("H:H"), Cells(i, "F").Value) + Application.WorksheetFunction.CountIf(Workbooks(1).Sheets(2).Range("H:H"), Cells(i, "F").Value)
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM