![](/img/trans.png)
[英]Excel VBA: how to solve Index and Match function type mismatch error
[英]INDEX MATCH in VBA issuing a type mismatch error
在下面的代碼上得到類型不匹配錯誤。 目的是將布爾值返回到一個單元格,該布爾值是通過比較2個不同范圍的2個輸出的結果而得出的。 一個范圍只是一個直接range()函數,另一個范圍是從INDEX MATCH中找到的結果。 MATCH函數給出錯誤,但我似乎無法弄清楚原因。
我在下面嘗試了2種不同的選擇。
dim i as long, j as long
Dim index As Variant
Dim compare As Variant
Dim bool As Boolean
i = 11
Do While i < RAGlastRow + 1
j = 41
Do While j < 44
RAGspreadsheet.Cells(i, j) = Application.IsError(Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
j = j + 1
index = Application.index(HistoryWS.Range(Cells(11, 7).Address, Cells(lastRow, 7).Address), Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
compare = RAGspreadsheet.Range("H" & i)
bool = index <> compare
RAGspreadsheet.Cells(i, j) = bool
j = j + 1
RAGspreadsheet.Cells(i, j) = RAGspreadsheet.Range("I" & i) <> Application.index(HistoryWS.Range(Cells(11, 8).Address, Cells(lastRow, 8).Address), Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
j = j + 1
Loop
i = i + 1
Loop
要么
dim i as long, j as long
i = 11
Do While i < RAGlastRow + 1
j = 41
Do While j < 44
RAGspreadsheet.Cells(i, j) = Application.IsError(Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
j = j + 1
RAGspreadsheet.Cells(i, j) = RAGspreadsheet.Range("H" & i) <> Application.index(HistoryWS.Range(Cells(11, 7).Address, Cells(lastRow, 7).Address), Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
j = j + 1
RAGspreadsheet.Cells(i, j) = RAGspreadsheet.Range("I" & i) <> Application.index(HistoryWS.Range(Cells(11, 8).Address, Cells(lastRow, 8).Address), Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
j = j + 1
Loop
i = i + 1
Loop
它的INDEX MATCH函數的MATCH部分似乎正在引發錯誤。
解釋我的評論。 這適用於您的所有代碼,盡管我將重點介紹index
。
你寫了:
index = Application.index(HistoryWS.Range(Cells(11, 7).Address, Cells(lastRow, 7).Address), Application.Match(RAGspreadsheet.Range("C" & i).Value, HistoryWS.Range("C11", "C" & lastRow), 0))
您沒有完全合格所有Cells()
,而是在Range("A1")
和Cells(1,1)
之間來回交換,這使得保持一致變得困難。 看到:
index = Application.index(HistoryWS.Range(HistoryWS.Cells(11, 7), HistoryWS.Cells(lastRow, 7)), Application.Match(RAGspreadsheet.Cells(i, 3).Value, HistoryWS.Range(HistoryWS.Cells(11, 3), HistoryWS.Cells(lastRow, 3)), 0))
我還從范圍中刪除了.Address
。
您可能應該使用With
語句,因此更容易閱讀:
With HistoryWS
index = Application.index(.Range(.Cells(11, 7), .Cells(lastRow, 7)), Application.Match(RAGspreadsheet.Cells(i, 3).Value, .Range(.Cells(11, 3), .Cells(lastRow, 3)), 0))
End With
請注意.
保持不變,以便HistoryWS
始終符合適當的范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.