excel 抛出变量不匹配错误

shyt4zoc  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(66)

我的代码中出现了“Run-time error '13':Type mismatch”错误:

Cells(i, "E").Value = .WorksheetFunction.Ceiling_Precise(Cells(i, "D").Value * Cells(1, "B").Value, 1)

字符串
我一直在盯着它,似乎看不到什么原因导致的错误。提前感谢。
我的变量是:

Dim i As Long, lRow As Long, subTotal As Long, percentageSum As Double, allFilled As Boolean, c As Integer


我尝试将i更改为RangeInteger,但不起作用。

编辑-这里有更多的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
  Call calculatePercentage
End Sub

Sub calculatePercentage()
  Dim i As Long, lRow As Long, subTotal As Long, percentageSum As Double, allFilled As Boolean, c As Integer
  lRow = Cells(Rows.Count, "D").End(xlUp).Row
  allFilled = True
  For i = 2 To lRow
    If Cells(i, "D").Value = "" And InStr(Cells(i, "C").Value, "Total") = 0 Then
      allFilled = False
    End If
  Next
  If allFilled Then
    With Application
    .EnableEvents = False
  
  
    Range("E2:E" & lRow).Clear
    For i = 2 To lRow
      If InStr(Cells(i, "C").Value, "Total") > 0 Then
        Cells(i, "D").Clear
      End If
    Next
    For i = 2 To lRow + 1
      If i > lRow And Not InStr(Cells(i, "C").Value, "Total") > 0 Then
        Exit For
      End If
      Cells(i, "E").Value = .WorksheetFunction.Ceiling(Cells(i, "D").Value * Cells(1, "B").Value, 1)
      percentageSum = percentageSum + Cells(i, "D").Value
      subTotal = subTotal + Cells(i, "E").Value
      If Cells(i, "D").Value = "" Then
        Cells(i, "D").Value = percentageSum
        Cells(i, "D").NumberFormat = "0.00%"
        Cells(i, "E").Value = subTotal
        If Not c > 0 Then
          percentageSum = 0
          subTotal = 0
          c = c + 1
        End If
      End If
    Next
    .EnableEvents = True
    End With
  End If
End Sub

编辑二:

Worksheet
我需要执行以下操作:
1.将每个范围的百分比相加,并将其放入“总计”列。
1.计算每一行的百分比:

  • 将其乘以左上角的Total number单元格
  • 四舍五入到小数点后0位
  • 将结果放在“值”列中相应的行中。

1.一旦对Total 1行执行了此操作,则需要重置公式,并对Total 2和Total 3执行相同的操作。
1.它需要是动态的,这样,如果用户插入或删除行,这将是考虑。

  • 乘法将是5,625,000(B1)x 14.25%(D3)= 801,563,然后5,625,000(B1)x 3.00%(D4)= 168,750,等等。
  • 步骤1中的小计应该在“值”列中。因此,801,563将在E3中,然后168,750将在E4中,依此类推。
  • 总计1小计的百分比和值总计需要始终位于其相应列中范围内最后一个值下方的单元格中(在本例中:D20和E20)
  • 总计2小计的百分比和值总和需要始终位于其各自列中范围内最后一个值的下方单元格中(在本例中:D27和E27)
  • 总计3的总计百分比是总计2的总计(在本例中:D27)+总计3的小计(在本例中:D27:D31),并且需要始终位于各自列中总计3范围内最后一个值的下方单元格中(在本例中为D32)。
  • Total 3的Value total是Total 2的总和(在本例中为E27)+Total 3的小计(在本例中为E27:E31),并且需要始终位于各自列中Total 3范围内最后一个值的下方单元格中(在本例中为E32)。-最后,所有这些都需要是动态的,以便在用户添加或删除行时考虑在内。
d8tt03nd

d8tt03nd1#

Sub calculatePercentage(ws As Worksheet)

    Dim lastrow As Long, r0 As Long
    Dim n As Long, i As Long, f As String

    On Error GoTo MyErrHandler
    Application.EnableEvents = False
    With ws
        lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
        r0 = 3
        For i = r0 To lastrow
            
            If LCase(.Cells(i, "C")) Like "total*" Then
                n = r0 - i
                f = "=Round(Sum(R[" & n & "]C:R[-1]C),0)"
                .Cells(i, "D").FormulaR1C1 = f
                r0 = i + 1
            End If
            ' value
            f = "=ROUND(R1C2 * RC[-1],0)"
            .Cells(i, "E").FormulaR1C1 = f
            
        Next
    End With
    
MyErrHandler:
    Application.EnableEvents = True

End Sub

字符串

aelbi1ox

aelbi1ox2#

我已经运行了各种可能性的排列,产生了几个不同的错误消息,更重要的是,我复制了您的错误消息。
你会得到错误信息,因为你的i变量被定义为一个字母,单词或其他非数字字符。你需要检查你的代码,找到i值被设置的地方,或者值被改变的地方,你会发现需要纠正的错误。
或者是B或D列中的某个电子表格单元格中有一个字符串或布尔值;检查i的开始值和结束值是否正确,例如检查是否试图乘以列标题。我注意到第二个相乘的值是Cells(1,“B”),第1行B列,也许应该是 * Cells(i,“B”)。

相关问题