VBA - 类型不匹配 - 代码



我一直在以各种排列方式玩弄这段代码,试图让某些东西工作。 if 语句的前半部分正常工作,但如果范围的总和为 0,则代码不会执行所需的计算。 我收到"类型不匹配"错误。

有什么想法吗?

n = ActiveWorkbook.Sheets.Count
For i = 2 To n
    If Application.Sum(Sheets(i).Range("O13:O33")) > 0 Then
    Range(Cells(13, i), Cells(19, i)).Value = Sheets(i).Range("P13:P19").Value
    Else
    Range(Cells(13, i), Cells(19, i)).Value = Sheets(i).Range("I13:I19") - (4 * Sheets(i).Range("K13:K19"))
    End If
Next i

此致敬意。

您不能对多单元格区域进行数学运算。

第一个基本上是移动一个数组,但您需要在范围内循环并一次对每个单元格进行数学运算。

Dim Rng As Range
n = ActiveWorkbook.Sheets.Count
With ActiveSheet
    For i = 2 To n
        If Application.Sum(Sheets(i).Range("O13:O33")) > 0 Then
            .Range(.Cells(13, i), .Cells(19, i)).Value = Sheets(i).Range("P13:P19").Value
        Else
            For Each Rng In .Range(.Cells(13, i), .Cells(19, i))
                Rng.Value = Sheets(i).Cells(Rng.Row, "I") - (4 * Sheets(i).Cells(Rng.Row, "K"))
            Next Rng
        End If
    Next i
End With

最新更新