VBA整数从单元格

在我的代码中,我一直有错误“运行时错误13 – types不匹配”当我把行从注释中获取从单元格的值放入整数(qtyCode = Cells(x,“L”)。Value),这消失了。 但我似乎无法找出为什么它是types不匹配。

列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 

我认为解决scheme将是容易的,我可以忽略最有可能的东西..

提前致谢,

大卫

这是代码,值输出仍然有问题,但没有更多的错误,所以这个问题解决了:)

 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 

不知道这是错误的来源,但请记住Cells()是基于0的。 这意味着要引用单元格F7 ,您必须使用Cells(6,"F")
所以你也许应该回顾一下FOR...NEXT语句,使用如下的语句:

 For x = 0 To numRow - 1 

为了避免这种差异,可读性更好的代码也可以使用Range("F" & x) 。 也许效率略低,但我觉得debugging更舒服。


编辑:单元格()是不是0基于,而是1基于。 抱歉的错误信息,但很高兴它有助于解决这个问题。

发生错误时添加debugging信息:

 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 

主要的问题是你必须声明所有用于引用列/行索引的variables,因为表格列/行数可以超过Integervariables的容量。

  • 习惯于将Option Explicit语句放在模块的最顶端,这样就迫使自己声明所有的variables并获得更多的代码控制权。

  • 使用完全合格的参考文件,直到工作表的人确定你处理的范围。

  • 改变你的行数(见下面的代码)

只要编码习惯有限,这些只是一些build议,其结果可能如下:

 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行“AM”,但从第1行迭代。

可能你可以想象一个彻底的情景