下标超出范围,为什么?

时间:2019-06-18 17:57:45

标签: excel vba

我正在写一个宏,它基于电子表格中的数据制作某种表,并且下标超出了特定行的范围。在代码下面,我标记了它发生的确切位置(1)。我试过放置另一个变量,例如l,然后像l = i这样执行,然后在i = l-1以下执行,但是它和以前一样。

Sub testo()

    Const cSheet As String = "Procenty"   ' spreadsheet name
    Const cRange As String = "A2:D71"     ' range
    Const cel As Long = 6             
    Const cCol As Variant = "A"           

    Dim vntS As Variant   ' tablica from
    Dim vntT As Variant   ' tablica to
    Dim i As Integer      ' row counter
    Dim emptyRow As Long  ' first empytrow



    Dim kom As Double, komz As Double, kredyt As Double

    Dim roz As Double, komr As Double, komn As Double nadplata
    Dim napis As String 
    Dim dz As Date, dw As Date 

    napis = "null"


    vntS = ThisWorkbook.Worksheets(cSheet).Range(cRange)

    'changing table size
    ReDim vntT(1 To UBound(vntS), 1 To cel)


    kredyt = 0
    For i = 1 To UBound(vntS)
If vntS(i, 1) = "" Then
dw = Date
Else
dz = vntS(i, 1)
End If

        ' values to variables
        dz = vntS(i, 1)
        komz = vntS(i, 2)
        dw = vntS(i, 3)
        kom = vntS(i, 4)

        '*******************************
        ' Start
        '*******************************

        If kom = komz Then

            vntT(i, 1) = dz
            vntT(i, 2) = komz
            vntT(i, 3) = dw
            vntT(i, 4) = kom


        ElseIf komz > kom Then

              vntT(i, 1) = dz
            vntT(i, 2) = komz
            vntT(i, 3) = dw
            vntT(i, 4) = kom
            i = i + 1
            komr = komz - kom
            vntT(i, 1) = dz
            vntT(i, 2) = komr
            vntT(i, 3) = dw
            vntT(i, 4) = kom
           i = i - 1

        ElseIf komz < kom Then

          vntT(i, 1) = dz
            vntT(i, 2) = komz
            vntT(i, 3) = dw
            vntT(i, 4) = kom
            i = i + 1
            komn = kom - komz
            vntT(i, 3) = dw <------- (1) here is the subscript out of range
            vntT(i, 4) = komn
            i = i - 1


       ' ElseIf komz > kom And dz >= dw Then
       ' roz = Abs(dz - dw)
        'komr = komz - kom
           ' vntT(i, 5) = napis
           ' kredyt = kredyt - komr

       ' ElseIf komz < kom And dw <= dz Then
      '  komn = kom - komz
       '     vntT(i, 5) = komn
        '    kredyt = kredyt + komn
       ' ElseIf komz < kom And dz < dw Then
       ' roz = Abs(dz - dw)
        '    vntT(i, 5) = procent(komz, roz)
        '    kredyt = kredyt - procent(komz, roz)
        End If

        '*******************************
        ' end
        '*******************************

    Next


    With ThisWorkbook.Worksheets(cSheet)

        emptyRow = .Columns(cCol).Find("*", , xlFormulas, _
                xlWhole, xlByColumns, xlPrevious).Row + 1
        'emptyRow = WorksheetFunction.CountA(.Columns(cCol)) + 1
        ' writing table out
        .Cells(emptyRow, cCol).Resize(UBound(vntT), UBound(vntT, 2)) = vntT
        .Cells(emptyRow, cCol) = kredyt
    End With

End Sub

0 个答案:

没有答案