簡體   English   中英

VBA:如何使用 for 循環將數組添加到鋸齒狀數組

[英]VBA: How to add arrays to a jagged array using a for loop

想象一下以下電子表格,但您不確定會有多少行:

示例電子表格

現在,您需要創建一個包含其他數組(稱為ArticleArray )的數組,使其輸出如下所示:

ArticleArray = {{"Fake Outlet 1","9/1/2020","Fake headline 1"},{"Fake Outlet 2","9/2/2020","Fake headline 2"},{"Fake Outlet 3","9/3/2020","Fake headline 3"}}

您可以在下面看到我已經編寫的代碼示例:

Sub Pull_News()

    Dim i As Long

    'Finding the last row of the news table and adding borders
    Dim lRow As Long
    lRow = Sheets("News Archive").Cells(Rows.Count, 1).End(xlUp).Row
    
    'Creating a jagged array that will storeeach article array
    Dim ArticleArray() As Variant

    'Lopping through each article and creating an array
    For i = 2 To lRow - 1
        
        'Creating a temporary news array
        Dim Article() As Variant
        
        'Filling Temporary Array
        Article = Sheets("News Archive").Range("A" & i & ":" & "C" & i).Value
        
        'Adding temporary array to the jagged array
            
        'change / adjust the size of array
        ReDim Preserve ArticleArray(1 To UBound(ArticleArray) + 1) As Variant
        
        ' add value on the end of the array
        ArticleArray(UBound(ArticleArray)) = Article
        
    Next i

End Sub

我在以下行收到以下錯誤消息:

ReDim Preserve ArticleArray(1 To UBound(ArticleArray) + 1) As Variant

Run-time error 9: Subscript out of range

  1. Article不是Range並且沒有.Value
  2. ReDim Preserve價格昂貴。 最好在循環外設置一次數組大小:
Dim ArticleArray() As Variant
ReDim ArticleArray(0 To lrow -2) 'size the array outside the loop

Dim j As Long

'Looping through each article and creating an array
For i = 2 To lRow
        
    'Creating a temporary news array
    Dim Article() As Variant
        
    'Filling Temporary Array
    Article = Sheets("News Archive").Range("A" & i & ":C" & i).Value
        
    'Adding temporary array to the jagged array
    ArticleArray(j) = Article
    j = j + 1
        
Next i

2D 與鋸齒狀陣列

編碼

Option Explicit

Sub Pull_News()
    
    ' Constants
    Const wsName As String = "News Archive"
    Const srcRange As String = "A:C"
    Const LastRowColumn As Long = 1
    Const FirstRow As Long = 2
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    ' Define worksheet.
    Dim ws As Worksheet
    Set ws = wb.Worksheets(wsName)

    ' Calculate last row.
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, _
                       ws.Range(srcRange).Columns(LastRowColumn).Column) _
                .End(xlUp).Row
    
    ' Define Source Range.
    Dim rng As Range
    Set rng = ws.Range(srcRange).Resize(LastRow - FirstRow + 1) _
                                .Offset(FirstRow - 1)
     
    ' Write values from Source Range to Source Array.
    Dim Data As Variant
    Data = rng.Value
    
    ' Write contents of Source Array to the Immediate window (CTRL+G).
    Dim i As Long
    Debug.Print "Source Array:"
    For i = 1 To UBound(Data, 1)
        Debug.Print Data(i, 1), Data(i, 2), Data(i, 3)
    Next i
    
    ' Define upper limits of arrays.
    Dim UB1 As Long
    UB1 = UBound(Data, 1)
    Dim UB2 As Long
    UB2 = UBound(Data, 2)
    
    ' Define jagged Articles Array.
    Dim Articles As Variant
    ReDim Articles(1 To UB1)

    ' Define Temporary Article Array.
    Dim TempArticle As Variant
    ReDim TempArticle(1 To 1, 1 To UB2)
    
    ' Copy values from Source Array to jagged Articles Array.
    Dim j As Long
    For i = 1 To UB1
        ' Copy values from Source Array to Temporary Article Array.
        For j = 1 To UB2
            TempArticle(1, j) = Data(i, j)
        Next j
        ' Copy Temporary Article Array to jagged Articles Array.
        Articles(i) = TempArticle
    Next i
    
    ' Write contents of jagged Articles Array to the Immediate window (CTRL+G).
    Debug.Print "Jagged Articles Array:"
    For i = 1 To UB1
        Debug.Print Articles(i)(1, 1), Articles(i)(1, 2), Articles(i)(1, 3)
    Next i
    
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM