首页 文章

Excel VBA如何将范围从不同的表格传递到正常的功能?

提问于
浏览
0

我需要使用Excel VBA中不同工作表的单元格范围 . 这是我的代码:

Function LastDayForClient(client As String, mnth As Integer, dates As range) As String

Dim dtRow As Date
Dim dt As range

dtRow = 0

For Each dt In dates
      ' If not that month that we are looking for - go next
     If month(dt.Value) = mnth Then
       ' If not that client - go next
        If Cells(dt.Row, dt.Column + 1).Value = client Then

          If dtRow = 0 Then
            dtRow = dt.Value
          ElseIf dtRow < dt.Value Then
            dtRow = dt.Value
          End If
        End If
      End If
Next dt

LastDayForClient = Format(dtRow, "dd.mm.yyyy")

End Function

这就是我想要使用公式的方式:

=LastDayForClient("Client_name";5;Diff_sheet!G6:G297)

我猜我正在以错误的方式使用传递 Range 参数,因为当在不同的工作表上使用公式时我得到了错误的值 . 当范围在同一张纸上时一切都很好 .

1 回答

  • 0

    问题是这条线

    If Cells(dt.Row, dt.Column + 1).Value = client Then
    

    单元格引用ActiveSheet,因此您需要告诉Cells引用正确的工作表 .

    Dim wks as worksheet
    set wks = dates.parent
    

    然后

    If wks.Cells(dt.Row, dt.Column + 1).Value = client Then
    

相关问题