【问题标题】:Loop Array from UBound to LBound and check values从 UBound 到 LBound 循环数组并检查值
【发布时间】:2019-01-25 00:17:39
【问题描述】:

我正在尝试创建一个数组,从 UBound 循环到 LBound 并使用以下代码检查值。

我在线收到错误消息:

 If arrPart(i) = strResult Then

运行时错误 9

我尝试在数组中导入的范围:

代码:

    Option Explicit

    Sub ArrayTest()

        Dim LastColumn As Long, CounterPart As Long, i As Long
        Dim arrPart As Variant
        Dim strResult As String

        With ThisWorkbook.Worksheets("Sheet1")

            LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column

            strResult = "N"

            'Set as an array the 4 last matches
            arrPart = .Range(Cells(1, LastColumn - 3), Cells(1, LastColumn))

            CounterPart = 0

            For i = UBound(arrPart) To LBound(arrPart) Step -1

                If arrPart(i) = strResult Then
                    CounterPart = CounterPart + 1
                Else
                    Exit For
                End If

            Next

        End With

    End Sub

有什么建议吗?

【问题讨论】:

  • arrPart 是一个二维数组,必须像arrPart(row, column) 一样使用。您可以将UBound(arrPart, 1) 用于row,将UBound(arrPart, 2) 用于column
  • If arrPart(1,i) = strResult Then 还要确保将父级附加到Range() 内的Cells(),否则如果该工作表不是活动工作表,则会出现错误:arrPart = .Range(.Cells(1, LastColumn - 3), .Cells(1, LastColumn))
  • 您还可以对数组进行双重转置以获得一维数组WorksheetFunction.Transpose(WorksheetFunction.Transpose(.Range(.Cells(1, LastColumn - 3), .Cells(1, LastColumn))))
  • 您可能需要查看以下帖子:stackoverflow.com/questions/26644231/…

标签: excel vba


【解决方案1】:

根据上述所有 cmets:

Option Explicit

Sub ArrayTest()

    Dim LastColumn As Long, CounterPart As Long, i As Long
    Dim arrPart As Variant
    Dim strResult As String

    With ThisWorkbook.Worksheets("Sheet1")

        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column

        strResult = "N"

        'Set as an array the 4 last matches
        arrPart = .Range(.Cells(1, 1), .Cells(1, LastColumn))

        CounterPart = 0

        For i = UBound(arrPart, 2) To LBound(arrPart, 2) Step -1

            If arrPart(1, i) = strResult Then
                CounterPart = CounterPart + 1
            Else
                Exit For
            End If

        Next

    End With

    Debug.Print CounterPart
End Sub

【讨论】:

    【解决方案2】:

    假设您有一个从B4 开始的单元格表。

    这是您找出表格大小、将值传输到数组并遍历它们的方法。

    Public Sub ArrayTest()
    
        Dim r_start As Range
        Set r_start = Range("B4")
    
        Dim i As Long, n As Long
        n = Range(r_start, r_start.End(xlToRight)).Columns.Count
    
        Dim arrPart() As Variant
        arrPart = r_start.Resize(1, n).Value
    
        Dim strResult As String
        strResult = "N"
    
        Dim counter As Long
        counter = 0
    
        For i = 1 To n
            If arrPart(1, i) = strResult Then
                counter = counter + 1
            Else
                Exit For
            End If
        Next i
    
        Debug.Print counter
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多