繁体   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