如何指定 sheet a worksheet 函数应该通过范围对象使用?

How to specify the sheet a worksheet function should use via a range object?

我有一些代码以字符串 "value1 - value 2"

的格式搜索 sheet(3) 中的一列值

value2 是 sheet(2) 中列中的第一个值,value1 是同一列中的值,在 sheet.

下方的单元格中

我的设置是:

在sheet(1)中,单元格C2:C6分别有值a-e

在 sheet(2) 中,单元格 C1 的值为 "yes",单元格 C2:C6 的值为 1-5 分别

在 sheet(3) 中,单元格 A2 的值为“4 - 是”

所以代码应该计算 sheet2 中第一个值为 yes 的列并查找值为 4 的单元格,并将结果放在 sheet(3) 上的单元格 B2 中

它实际上做的是找到“是”列(C 列)并在 sheet(1) 上搜索同一列(因此消息框显示字母而不是数字)。

有什么方法可以更精确地指定 countif 函数使用的 sheet?

我在 Windows 7

上使用 Excel 2000
Private Sub test_click()

scenario_count = 6
Dim i As Integer
i = 1

Sheets(2).Select
For j = 2 To 24
    If Sheets(2).Cells(1, j).Value = Right(Sheets(3).Cells(i + 1, 1).Value, Len(Sheets(3).Cells(i + 1, 1).Value) - InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 1) Then
        MsgBox ("number of scenarios is " & scenario_count)
        MsgBox ("value searching for is " & "'" & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2) & "'")
        MsgBox ("Range searched is " & Range(Cells(2, j), Cells(scenario_count, j)).Address & " in " & ActiveSheet.Name)
        MsgBox ("Number of occurrences " & Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2)))

        Sheets(2).Select
        Sheets(3).Cells(i + 1, 2).Value = Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))

        For Each c In Range(Cells(2, j), Cells(scenario_count, j))
            MsgBox ("comparing " & c.Address & " " & c.Value & " with " & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
        Next c

    GoTo endofif2

    End If
Next
endofif2:
End Sub

在 'WorksheetFunction.CountIf(Range(Cells(2, j)' 的位置,只需在范围引用前插入 sheet,如下所示:

Sheets(2).Range(Sheets(2).Cells(2, j), Sheets(2).Cells(scenario_count, j))

EDIT 完整公式引用了单元格和范围函数的 sheet 公然取自@Rory 的评论。