在我的代码中,我一直有错误"运行时错误13 - 类型不匹配" 当我将注释中的行放入单元格中以获取整数值时(qtyCode = Cells(x," L")。值),这就消失了。但我似乎无法找出它的类型不匹配的原因。
列L在我的Excel文件中设置为数字。
Sub counting()
Dim code As String
Dim lookup As String
Dim qtyCode As Integer
Dim qtyLookup As Integer
Dim numRows As Integer
numRows = Range("AM2", Range("AM2").End(xlDown)).Rows.Count
For x = 1 To numRows
code = Cells(x, "AM").Text
qtyCode = Cells(x, "L").Value 'error here
For y = 1 To numRows
lookup = Cells(y, "AM").Text
If (code = lookup) Then
qtyLookup = CInt(Cells(y, "L").Text) 'error here
qtyCode = qtyCode + qtyLookup
End If
ActiveCell.Offset(1, 0).Select
Next
Cells(x, "AN").Value = qtyCode
ActiveCell.Offset(1, 0).Select
Next
End Sub
我认为解决方案很简单,而且我很容易忽略一些事情......
提前致谢,
大卫
这是代码,值输出仍有问题,但没有更多错误,所以这个问题就解决了:)
Sub counting()
Dim code As String
Dim lookup As String
Dim a As Long
Dim b As Long
Dim c As Long
Dim numRows As Integer
numRows = Range("AM2", Range("AM2").End(xlDown)).Rows.Count
For x = 2 To numRows
code = Cells(x, "AM").Text
a = CLng(Cells(x, "L").Value)
For y = 2 To numRows
lookup = Cells(y, "AM").Text
If (code = lookup) Then
b = CLng(Cells(y, "L").Value)
c = a + b
End If
Next
Cells(x, "AN").Value = c
Next
End Sub
答案 0 :(得分:1)
不确定这是否是您的错误来源,但请记住Cells()
基于0。这意味着要引用单元格F7
,您必须使用Cells(6,"F")
因此,您应该相应地使用以下内容查看FOR...NEXT
声明:
For x = 0 To numRow - 1
对于避免这种差异的更易读的代码,您还可以使用Range("F" & x)
。可能效率稍差但我觉得调试更舒服。
编辑:Cells()不是基于0,而是基于1。对于错误信息感到抱歉,但很高兴它有助于解决问题。
答案 1 :(得分:1)
发生错误时添加调试信息:
Sub counting()
On Error GoTo ErrorTrap
Dim code As String
Dim lookup As String
Dim qtyCode As Integer
Dim qtyLookup As Integer
Dim numRows As Integer
numRows = Range("AM2", Range("AM2").End(xlDown)).Rows.Count
For x = 1 To numRows
code = Cells(x, "AM").Text
mydbgval=Cells(x, "L").Value
qtyCode = Cells(x, "L").Value 'error here
For y = 1 To numRows
lookup = Cells(y, "AM").Text
If (code = lookup) Then
mydbgval=CInt(Cells(y, "L").Text)
qtyLookup = CInt(Cells(y, "L").Text) 'error here
qtyCode = qtyCode + qtyLookup
End If
ActiveCell.Offset(1, 0).Select
Next
Cells(x, "AN").Value = qtyCode
ActiveCell.Offset(1, 0).Select
Next
Exit Sub
ErrorTrap:
Beep
MsgBox "FAILED" & Chr(13) & "Error number: " & Err & Chr(13) & Error(Err) & Chr(13) & "dbgval:<" & mydbgval & ">"
End Sub
答案 2 :(得分:1)
主要问题是您必须声明用于引用Long
类型的列/行索引的所有变量,因为工作表列/行数可能超过Integer
变量的容量。
另外
养成将Option Explicit
语句放在模块顶部的习惯,从而迫使自己声明所有变量并更好地控制代码。
使用完全限定的参考资料直至工作表,以确定您处理的范围。
更改计算行的方式(请参阅下面的代码)
Option Explicit '<== always use this
Sub counting()
Dim code As String
Dim lookup As String
Dim qtyCode As Integer
Dim qtyLookup As Integer
Dim x As Long, y As Long
Dim numRows As Long
With ThisWorkbook.Worksheets("MySheet") '< change it as per your needs
numRows = .Range("AM2", .Cells(.Rows.Count, "AM").End(xlUp)).Rows.Count 'get the last non blank row of column "AM"
For x = 1 To numRows
code = Cells(x, "AM").Text
qtyCode = Cells(x, "L").Value
For y = 1 To numRows
lookup = Cells(y, "AM").Text
If (code = lookup) Then
qtyLookup = CInt(Cells(y, "L").Text)
qtyCode = qtyCode + qtyLookup
End If
ActiveCell.Offset(1, 0).Select
Next y
Cells(x, "AN").Value = qtyCode
ActiveCell.Offset(1, 0).Select
Next x
End With
End Sub
最后,我没有掌握代码的逻辑:你开始计算列的第2行和#34; AM&#34;然后从第1行迭代。
可能是你可以想象一个彻底的场景