简体   繁体   English

Excel VBA:下标超出范围

[英]excel vba: subscript out of range

the vba code is: VBA代码是:

    Sub D()
    Dim a As String
    Dim wb As Workbook
      Dim file As Variant

       Dim arr1() As Variant
      Dim arr2() As Variant
      Dim arr3() As Variant
      Dim arr() As Variant
      Dim arr4() As Variant
       Dim arr5() As Variant
       Dim arr6() As Variant

       Dim t As Integer



            ActiveWorkbook.Activate
            ActiveSheet.Activate
            arr4 = Range("J2:J256")
            arr5 = Range("K2:K256")
            arr6 = Range("L2:L256")


         ActiveSheet.Activate


            For Row = 1 To UBound(arr4, 1)
                If arr4(Row, 10) = "IS" And arr5(Row, 11) = "IS" And arr6(Row, 12) = "IS" Then
                Cells(Row + 1, 13) = "UPDATE AB SET S=" & Cells(Row + 1, 6) & "WHERE C=" & Cells(Row + 1, 3) & ";"
                End If
            Next Row
    End Sub

I am getting error as Subscript Out of range at arr4(Row,10)= when debugged.Can u help in rectifying the error so that the code may be able to function correctly. 调试时出现下标超出范围,错误为arr4(Row,10)=。您能帮助纠正错误,以便代码正常运行吗?

Regarding the out of range error, since arr4, arr5 and arr6 only contain one column you cannot access for example the 10th column of them (which is done in your code by arr4(Row, 10) . Does you code work as wanted if you use the following? 关于超出范围的错误,由于arr4,arr5和arr6仅包含一列,因此您无法访问它们的第10列(这在代码中由arr4(Row, 10) 。使用以下内容?

Sub D()
      Dim a As String
      Dim wb As Workbook
      Dim file As Variant
      Dim Row As Integer
      Dim arr1() As Variant
      Dim arr2() As Variant
      Dim arr3() As Variant
      Dim arr() As Variant
      Dim arr4() As Variant
      Dim arr5() As Variant
      Dim arr6() As Variant
      Dim t As Integer

            ActiveWorkbook.Activate
            ActiveSheet.Activate
            arr4 = Range("J2:J256")
            arr5 = Range("K2:K256")
            arr6 = Range("L2:L256")

            For Row = 1 To UBound(arr4, 1)
                If arr4(Row, 1) = "IS" And arr5(Row, 1) = "IS" And arr6(Row, 1) = "IS" Then
                     Cells(Row + 1, 13) = "UPDATE AB SET S=" & Cells(Row + 1, 6) & "WHERE C=" & Cells(Row + 1, 3) & ";"
                End If
            Next Row
    End Sub

I think the problem lies in your understanding of the array. 我认为问题出在您对数组的理解上。 You are using absolute cell references for your array (which is relative). 您正在为数组使用绝对单元格引用(相对的)。 The first index of an array, ie (1, 1), references the first cell in your range, so for Range("K2:K256") arr(1, 1) will be referencing the value of cell "K2", arr(10, 1) will be referencing the value of cell "K11", etc. 数组的第一个索引(即(1,1))引用您范围内的第一个单元格,因此对于Range("K2:K256") arr(1, 1)将引用单元格“ K2”的值, arr(10, 1)将引用单元格“ K11”的值,依此类推。

As LMM9790 points out, if you wanted to keep your code structure as is then it could simply be written as: 正如LMM9790所指出的那样,如果您希望保持代码结构不变,则可以将其编写为:

        If arr4(Row, 1) = "IS" And arr5(Row, 1) = "IS" And arr6(Row, 1) = "IS" Then
            Cells(Row + 1, 13) = "UPDATE AB SET S=" & Cells(Row + 1, 6) & "WHERE C=" & Cells(Row + 1, 3) & ";"
        End If

However, I'd have to ask why you need so many arrays, one for each column? 但是,我不得不问为什么您需要这么多的数组,每一列一个? Given that arr4, 5 and 6 all have the same row dimension, you could simply have one array that contains all of the columns. 假设arr4、5和6都具有相同的行维,则可以简单地使一个数组包含所有列。 Moreover, you could have one array for the entire dataset, amend the applicable value, then rewrite the array to the Worksheet . 此外,您可以为整个数据集使用一个数组,修改适用的值,然后将该数组重写为Worksheet

Elsewhere the code is a little odd. 其他地方的代码有点奇怪。 Is there a reason, for example, that you would activate an active sheet and book? 例如,您是否有理由激活活动的工作表和书本? You also have several unused variables - are you intending to use these later? 您还有几个未使用的变量-您打算以后使用这些变量吗?

Your whole code could be simplified to this: 您的整个代码可以简化为:

Sub D()
    Dim ws as Worksheet
    Dim r As Integer
    Dim v As Variant

    Set ws = ActiveWorkbook.Worksheets("Sheet1") 'name as appropriate
    v = ws.Range("C2:M256").Value2

    For r = 1 To UBound(v, 1)
        If r < Ubound(v, 1) then
            If v(r, 8) = "IS" And v(r, 9) = "IS" And v(r, 10) = "IS" Then
                 v(r + 1, 11) = "UPDATE AB SET S=" & v(r + 1, 4) & _
                                " WHERE C=" & v(r + 1, 1) & ";"
            End If
        End If
    Next

    '...

    ws.Range(("C2:M256").Value = v
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM