简体   繁体   English

我在 VBA 的 for 循环中出现类型不匹配错误

[英]I get type mismatch error in for loop in VBA

'find the total of ages and anticor level to find the average For i = 1 To lastCellNumber totalAge = totalAge + Cells(i, 4) totalAnticorLevel = totalAnticorLevel + Cells(i, 6) Next i

Using FunThomas's suggestion:使用 FunThomas 的建议:

'find the total of ages and anticor level to find the average

For i = 1 To lastCellNumber
    if IsNumeric(Cells(i, 4).value) as sNumeric(totalAge) then
      totalAge = totalAge + Cells(i, 4)
    end if
   if IsNumeirc(totalAnticorLevel) and IsNumeric(Cells(i, 6).value) then
    totalAnticorLevel = totalAnticorLevel + Cells(i, 6)
    end if
Next i

But that would not calculate the cases where one of the parcels is not numeric, which would be your next problem to solve.但这不会计算其中一个地块不是数字的情况,这将是您要解决的下一个问题。

When you do当你这样做

totalAge + Cells(i, 4)

You're adding a value ( totalAge ) with a CELL ( Cells(i, 4) ).您正在使用 CELL( Cells(i, 4) )添加一个值( totalAge )。 You need to use the VALUE of the cell like:您需要使用单元格的值,例如:

totalAge + Cells(i, 4).value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM