在另一个工作表中调用范围(作为变量)

时间:2019-05-26 13:11:33

标签: excel vba

因此,我将范围设置为变量

Set Range_Count = Range("A2", Range("A2").End(xlDown))

现在我正在尝试使用VBA在WorksheetFunction中调用它,而无需选择工作表

'This is what I don't want to do anymore
Sheets(1).Select
Count_Product = Application.WorksheetFunction.CountIf(Range_Count, "<>")

'This is what I'm wondering if it's possible to be done
Count_Product = Application.WokrsheetFunction.CountIf(Sheets(2).Range_Count, "<>")

但是它似乎不起作用。有办法吗?

2 个答案:

答案 0 :(得分:0)

它是这样工作的:

Set Range_Count = Sheets(2).Range("A2", Range("A2").End(xlDown)) 'full qualify the range with it's sheet
Count_Product = Application.WorksheetFunction.CountIf(Range_Count, "<>") 'then use the range

答案 1 :(得分:0)

因此,我只是通过将纸页放在Application之前来解决此问题

Dim Range_Count As Range
Set Range_Count = Range("A2", Range("A2").End(xlDown))

Sheets(2).Application.WorksheetFunction.CountIf(Range_Count, "<>")

比我想象的容易