VBA - 容量溢出错误

时间:2018-05-30 14:07:38

标签: excel vba excel-vba

我有以下代码导致

  

容量溢出错误类型6

由于'------------------------------------'之间的代码。我将非常感谢你的帮助:)!

我无法通过其他讨论解决这个问题。

Sub Calculate_Mix()
    Dim rngUsernameHeader As Range
    Dim rngHeaders As Range
    Dim rngAddress As Range
    Dim rng_R1 As Range
    Dim rng_delisted As Range
    Dim ws As Worksheet
    Dim value As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set rngHeaders = Range("1:1") 'Looks in entire first row.
    Set rngUsernameHeader = rngHeaders.Find(what:="VAl MAT'Mar18", After:=Cells(1, 1))

    rngUsernameHeader.Offset(0, 1).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 1).value = "Delisted"

    rngUsernameHeader.Offset(0, 2).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 2).value = "New Launches"

    rngUsernameHeader.Offset(0, 3).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 3).value = "Price_17"

    rngUsernameHeader.Offset(0, 4).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 4).value = "Price_18"

    For Each Cel In Range("F2", Range("F2").End(xlDown))
    If Cel > 0 And Cel.Offset(0, 1) = 0 Then
            Cel.Offset(0, 2).value = Cel.value
    Else: Cel.Offset(0, 2).value = 0
    End If
    If Cel = 0 And Cel.Offset(0, 1) > 0 Then
            Cel.Offset(0, 3).value = Cel.Offset(0, 1).value
    Else: Cel.Offset(0, 3).value = 0
    End If
    Next Cel

    '------------------------------------'
    For Each Cel In Range("H2", Range("H2").End(xlDown))
    If Cel = 0 And Cel.Offset(0, 1) = 0 Then
        value = Cel.Offset(0, -2).value / Cel.Offset(0, -4).value
        Cel.Offset(0, 2).value = value
    Else: Cel.Offset(0, 2).value = 0
    End If
    Next Cel
   '------------------------------------'

End Sub

1 个答案:

答案 0 :(得分:2)

您已将value(顺便说一下,错误的变量名称)声明为Long。如果您尝试将十进制值分配给整数变量,则会出现溢出错误(这可能会发生在此行中:

value = Cel.Offset(0, -2).value / Cel.Offset(0, -4).value

尝试将value声明为Double

更好的是,重命名并将dMyValue声明为Double