![](/img/trans.png)
[英]Excel vba how to pass range to a function retrieve data from mysql
[英]Excel VBA How to pass range from different sheet to function properly?
我需要使用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
參數,因為在另一張紙上使用公式時,我得到了錯誤的值。 當范圍在同一張紙上時,一切都很好。
問題是線
If Cells(dt.Row, dt.Column + 1).Value = client Then
單元格引用ActiveSheet,因此您需要告訴單元格引用正確的工作表。
Dim wks as worksheet
set wks = dates.parent
接着
If wks.Cells(dt.Row, dt.Column + 1).Value = client Then
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.