【问题标题】:VBA to correctly loop through an arrayVBA正确循环遍历数组
【发布时间】:2017-05-13 10:29:50
【问题描述】:

我已经阅读了许多数组摘要,但我仍然迷失方向并寻求非常感谢的帮助。我已经成功创建了一个非数组宏,它在我的 ws 中复制一行,并在该父行下方放置三个副本。它对 ws 中的每一行都执行此操作。

例如

From:

ColA     ColB
Tom      Tent
Barry    Stove

To:

ColA     ColB
Tom      Tent
Tom      Tent
Tom      Tent
Tom      Tent
Barry    Stove
Barry    Stove
Barry    Stove
Barry    Stove

有 > 4000 行需要循环。我的代码工作正常,但速度很慢。所以我读到将 ws 放入数组中更好,然后循环遍历数组。这是我迷失数组的地方;当我将 ws 带入数组时,如何执行此复制并粘贴 x 3?我在下面写了一些代码,但不确定如何进一步执行。非常感谢。

Sub LoadDataintoArray()

Dim StrArray As Variant
Dim TotalRows As Long



TotalRows = Rows(Rows.Count).End(xlUp).Row
StrArray = Range(Cells(1, 1), Cells(TotalRows, 1)).Value

MsgBox "Loaded " & UBound(StrArray) & " items!"

'HERE I NOW WISH TO COPY EACH ROW IN THE WS (EXCEPT HEADER) AND PASTE THREE COPIES OF THAT ROW IMMEDIATELY BELOW THE PARENT ROW

'CODE I USED NOT USNG AN ARRAY IS BELOW
'
'    lRow = 2
'    Do While (Cells(lRow, "B") <> "")
'
'        RepeatFactor = 4
'
'        Range(Cells(lRow, "A"), Cells(lRow, "G")).Copy
'
'        Range(Cells(lRow + 1, "A"), Cells(lRow + RepeatFactor - 1, "G")).Select
'
'        Selection.Insert Shift:=xlDown
'
'           lRow = lRow + RepeatFactor - 1
'
'        lRow = lRow + 1
'    Loop
'

End Sub

【问题讨论】:

    标签: arrays excel vba loops


    【解决方案1】:

    读取数组比读取单元格值要快一些。真正的性能提升是将数据写回工作表。

    一如既往,我建议在 Youtube 上观看 Excel VBA Introduction。这是相关视频:Part 25 - Arrays

    Sub RepeatData()
        Dim Data As Variant, Data1 As Variant
        Dim x As Long, x1 As Long, x2 As Long, y As Long
    
        Data = Range("A2:G2", Range("B" & Rows.Count).End(xlUp))
        ReDim Data1(1 To UBound(Data, 1) * 4, 1 To UBound(Data, 2))
    
        For x = 1 To UBound(Data, 1)
            For x1 = 1 To 4
                x2 = x2 + 1
                For y = 1 To UBound(Data, 2)
                    Data1(x2, y) = Data(x, y)
                Next
            Next
        Next
    
        Range("A2:G2").Resize(UBound(Data1, 1)).Value = Data1
    
    End Sub
    

    【讨论】:

    • 非常感谢分享。这在我需要三行的地方增加了四行;不知道在哪里修改代码只是为了添加三个。谢谢。
    • Thomas,是的,我确实尝试过,而且效果很好。因为我需要四行,所以我还没有阅读代码并知道如何修改它以获得四行。我通过反复试验来玩它......更多的错误方面。谢谢。
    • @Andes2016 抱歉,我错过了阅读您的问题和评论。编码愉快!
    • 感谢修改!效果很好。
    【解决方案2】:

    如果您决定更改重复次数或希望每行重复的列数,此代码将更加灵活。

    Sub test1()
    
      'Set your input range to include all of the rows and all of the columns to repeat
      Dim StrArray As Variant
      StrArray = Range("A2:B5")
    
      Const numRepeats As Long = 4
      Const outputColumnStart As Long = 4
    
      Dim rowCounter As Long
      Dim colCounter As Long
    
      'Dimension a new array and populate it
      ReDim newArray(LBound(StrArray, 1) To UBound(StrArray, 1) * numRepeats, LBound(StrArray, 2) To UBound(StrArray, 2))
    
      For rowCounter = LBound(StrArray, 1) To UBound(StrArray, 1)
        Dim repeatCounter As Long
        For repeatCounter = 0 To numRepeats - 1
          For colCounter = LBound(StrArray, 2) To UBound(StrArray, 2)
            newArray(((rowCounter - 1) * numRepeats + 1) + repeatCounter, colCounter) = StrArray(rowCounter, colCounter)
          Next colCounter
        Next
      Next rowCounter
    
      'Write the values to the sheet in a single line.
      With ActiveSheet
        .Range(.Cells(1, 4), .Cells(UBound(newArray, 1), outputColumnStart + UBound(newArray, 2) - 1)).Value = newArray
      End With
    End Sub
    

    【讨论】:

    • 感谢您的代码分享和解释。
    【解决方案3】:

    你可以试试这个

    Option Explicit
    Sub Main()
        Dim Data As Variant
        Dim x As Long
    
        With Range("A2:G2", Range("B" & Rows.count).End(xlUp))
            Data = .Value
            For x = 1 To UBound(Data, 1)
                .Rows(4 * (x - 1) + 1).Resize(4) = Application.index(Data, x, 0)
            Next
        End With
    End Sub
    

    利用了我从 Thomas Inzina 那里知道的 this trick

    【讨论】:

    • 非常感谢分享。这有效,但给出了三行。您能解释一下在哪里修改以提供四行吗?
    • 我想我学会了如何修改以给出四行:Rows(4 * (x - 1) + 1).Resize(4) = Application.index(Data, x, 0)。正确的?你能解释一下最后发生了什么吗?
    • 不客气且正确:Rows(4 * (x - 1) + 1).Resize(4) = Application.index(Data, x, 0) 将复制四次(我将编辑答案)。至于解释: 1).Rows(3 * (x - 1) + 1) 引用引用范围的每第三行(Range("A2:G2", Range("B" &amp; Rows.count).End(xlUp)):第 1、4、7 ... 2 行).Resize(3) 将其扩展到三行。 3)Application.index(Data, x, 0) 获取Data 数组xth 行并将其分配给三行范围。最后,您可能希望将答案标记为已接受,谢谢!
    • 感谢插件...大声笑。 +1,因为我喜欢迄今为止最容易阅读和修改的。
    • @ThomasInzina,顺便说一句,你有没有成功地让年度技巧真正一次性从数组中写入不止一行(或一列)?跨度>
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多