簡體   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

使用 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

但這不會計算其中一個地塊不是數字的情況,這將是您要解決的下一個問題。

當你這樣做

totalAge + Cells(i, 4)

您正在使用 CELL( Cells(i, 4) )添加一個值( totalAge )。 您需要使用單元格的值,例如:

totalAge + Cells(i, 4).value

暫無
暫無

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

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