当行号和数组号不匹配时,循环返回一个数组。嵌套循环问题

时间:2019-07-08 13:58:24

标签: arrays excel vba

我有一个问题,我用变量创建了一个数组,我想在单独的列中输入数组中的值,该列与数组的行索引不匹配。

我想遍历一列,并且想从一个与该列的行索引不对应的数组中返回一个值。例如,这可能是返回第六行上的Array的第一个值。

我认为我的问题可能在于我不知道如何设置嵌套循环。

非常感谢您的帮助

我这样创建了我的数组

Sub arraytest()
    Dim MonthArray() As String
    Dim Lastrow As Long
    Dim StartRow As Byte
    StartRow = 2
    Dim r As Byte

    Lastrow = Range("B" & StartRow).CurrentRegion.Rows.count

    If Lastrow > 0 Then
        ReDim MonthArray(StartRow To Lastrow)
        For r = StartRow To Lastrow
            MonthArray(r) = Range("C" & r).Value
        Next r
    End If

End Sub

所以如果我的数组中有值

MonthArray()
    Month 1
    Month 2
    Month 3
    Month 4
    Month 5
    Month 6

那么一个不考虑行索引的简单循环就是

For i = StartRow To Lastrow

If (Cells(i, "A").Value = "USA:" or Cells(i, "A").Value = "EU:") Then _
    Cells(i, "B").Value = " " Else Cells(i, "B").Value = MonthArray(i)  <<<
Next i

这将按此顺序返回一个表

1     USA:
2     Data  MonthArray(2)
3     Data  MonthArray(3)
4     EU:
5     Data  MonthArray(5)
6     Data  MonthArray(6)

但是我需要像这样返回数组:

1     USA:
2     Data  MonthArray(1)
3     Data  MonthArray(2)
4     EU:
5     Data  MonthArray(3)
6     Data  MonthArray(4)

因此,在这种情况下,如果A列中的值不是USA或EU,我想从Array中添加值

我尝试过的是这个

r = 1 
For i = StartRow To Lastrow 
If (Cells(i, "A").Value = "USA" or Cells(i, "A").Value = "EU") Then _
    Cells   (i, "B").Value = " " Next i Else Cells(i, "B").Value = MonthArray (r) <<<
    r = r + 1
Next i

但是,我想要

r = r + 1

仅在(Cells(i,“ A”)。Value =“ USA”或Cells(i,“ A”)。Value =“ EU”)时出现

我们非常感谢您的帮助

2 个答案:

答案 0 :(得分:2)

如果您的MonthArray具有连续的范围,则不必担心循环,只需使用:

Dim MonthArray() As Variant, StartRow as Long, LastRow as Long
StartRow = 2
Lastrow = Cells(StartRow, "B").CurrentRegion.Rows.count
MonthArray = Range(Cells(StartRow, "C"), Cells(LastRow, "C")).Value

然后我们进入使用数组的过程,就像您的代码所示:

Dim r as Long, i as Long
r = 1 
For i = StartRow To Lastrow 
    If UCase(Cells(i, "A").Value) = "USA" or UCase(Cells(i, "A").Value) = "EU" Then
        Cells(i, "B").Value = " " 
    Else 
        Cells(i, "B").Value = MonthArray(r,1)
        r = r + 1
    End If
Next i

向下移动时,需要r = r + 1。


编辑1:

确保添加工作表引用。根据测试得出的假设,如果我根据B列确定LastRow,例如,我不想覆盖B中的单元格,例如:

With Sheets("MonthSource")
    Dim MonthArray() As Variant, StartRow as Long, LastRow as Long
    StartRow = 2
    Lastrow = .Cells(StartRow, "B").CurrentRegion.Rows.count
    MonthArray = .Range(.Cells(StartRow, "C"), .Cells(LastRow, "C")).Value
End With
With Sheets("Destination")
    Dim r as Long, i as Long
    r = 1 
    For i = StartRow To Lastrow 
        If UCase(.Cells(i, "A").Value) = "USA" or UCase(.Cells(i, "A").Value) = "EU" Then
            .Cells(i, "B").Value = " " 
        Else 
            .Cells(i, "B").Value = MonthArray(r,1)
            r = r + 1
        End If
    Next i
End With

答案 1 :(得分:1)

类似的事情应该对您有用:

Sub tgr()

    Dim ws As Worksheet
    Dim MonthArray() As Variant
    Dim StartRow As Long
    Dim LastRow As Long
    Dim i As Long, r As Long

    'Always fully qualify workbook and worksheet you're working with, change this as necessary
    Set ws = ActiveWorkbook.ActiveSheet

    StartRow = 2
    LastRow = ws.Cells(StartRow, "B").CurrentRegion.Rows.Count

    'Load the values of column C into an array directly, no loop required
    With ws.Range(ws.Cells(StartRow, "C"), ws.Cells(LastRow, "C"))
        If .Row < StartRow Then Exit Sub    'No data
        If .Cells.Count = 1 Then
            'Only a single value found in column C, force array type by manually redimming and adding the value
            ReDim MonthArray(1 To 1, 1 To 1)
            MonthArray(1, 1) = .Value
        Else
            'Multiple values found in column C, can insert values into array directly
            MonthArray = .Value
        End If
    End With

    'Initialize your array index counter variable at 0 to start
    r = 0

    'Begin loop of rows
    For i = StartRow To LastRow
        'Check contents of column A
        Select Case UCase(Trim(ws.Cells(i, "A").Value))
            Case "USA:", "EU:"
                'do nothing

            Case Else
                'increase array index counter variable
                r = r + 1

                'Output the appropriate array value to column B
                ws.Cells(i, "B").Value = MonthArray(r, 1)

        End Select
    Next i  'advance row counter

End Sub