簡體   English   中英

Excel VBA 匹配函數返回錯誤 2042

[英]Excel VBA Match function returns Error 2042

我在 VBA 子例程中的變量 findDeptrow 的匹配函數中收到錯誤 2042。 嘗試更改數據類型但沒有運氣。

感謝您的回答!

這是代碼:

Sub calculatechange()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Integer
Dim findDeptrow As Variant
Dim findTypecol As Variant

Set ws1 = Workbooks("woorkbook.xlsm").Worksheets("works2")
Set ws2 = Workbooks("workbook.xlsm").Worksheets("works2")

Dim lastrow As Long
lastrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row

ws1.Range("D3:H11").ClearContents

For i = 2 To lastrow
findDeptrow = Application.Match(ws2(i, 5), ws1.Columns(2), 0)
findTypecol = Application.Match(ws2.Cells(i, 13), ws1.Rows(2), 0)
ws1.Cells(CInt(findDeptrow), CInt(findTypecol)) = ws1.Cells(CInt(findDeptrow), CInt(findTypecol)) + ws2.Cells(i, 14)
Next i

End Sub

也許這會解決問題:

findDeptrow = Application.Match(ws2.Cells(i, 5).Value, ws1.Columns(2), 0)
findTypecol = Application.Match(ws2.Cells(i, 13).Value, ws1.Rows(2), 0)

您需要傳遞一個值作為第一個參數: https : //docs.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.match

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM