How to print two dimensional array in Immediate window in VBA?

后端 未结 6 1094
别那么骄傲
别那么骄傲 2020-12-30 01:20

How to print two dimensional array in Immediate window in VBA ? Does it exist any generic method for doing this ? Some method for ploting one row of array per line in Immedi

相关标签:
6条回答
  • 2020-12-30 01:40

    I made a simple loop to do this for anybody's reference:

    Sub WriteArrayToImmediateWindow(arrSubA As Variant)
    
    Dim rowString As String
    Dim iSubA As Long
    Dim jSubA As Long
    
    rowString = ""
    
    Debug.Print
    Debug.Print
    Debug.Print "The array is: "
    For iSubA = 1 To UBound(arrSubA, 1)
        rowString = arrSubA(iSubA, 1)
        For jSubA = 2 To UBound(arrSubA, 2)
            rowString = rowString & "," & arrSubA(iSubA, jSubA)
        Next jSubA
        Debug.Print rowString
    Next iSubA
    
    End Sub
    
    0 讨论(0)
  • 2020-12-30 01:41

    If this is for debugging purposes, it's not convenient to write a macro to view the contents of the array during program execution. Might even cause problems.

    For debugging during program execution, you'll want a code-free method you can use across all VB projects, to spy the contents of your array.

    1. In VBA IDE, click View menu > Locals Window
    2. In Local pane, find the array-name.
    3. Expand the nodes to find your values. The nodes will differ, depending on the type of array.

    In this example, "aLookList" is a variant array. The values under "Value2" come from a range.

    Another answer here suggests using the Watch pane. This is similar to my answer, but poster did not explain that you can spy the entire array (all cells) by simply adding the array name to Watch. Then , drill down nodes. The advantage of the Locals Window over the Watch Window, is that with Locals pane, you do not have to manually add the array to the pane, it's already there. So it's a bit less effort.

    0 讨论(0)
  • 2020-12-30 01:48

    No, you will either need to;

    • Create & call a function that loops & prints it out to the debug window.
    • If this is for debugging, right click the variable in the IDE & "Add Watch" which will bring up a window that will track changes to the value of the array & display its content when a breakpoint is hit.
    0 讨论(0)
  • 2020-12-30 01:49

    I wrote my own function for this. print_r() for VBA. It can evaluate Arrays, Dictionaries, Collection, MatchCollection etc.. The whole also nested. In addition, the data types are given and special characters are displayed in strings as Unicode.

    http://wiki.yaslaw.info/doku.php/vba/functions/print_r/index

    print_r(array(1,2,3,array("A","B")))
    <Variant()>  (
        [0] => <Integer> 1
        [1] => <Integer> 2
        [2] => <Integer> 3
        [3] => <Variant()>  (
            [0] => <String> 'A'
            [1] => <String> 'B'
        )
    )
    
    print_r([{1,2;3,4}])
    <Variant()>  (
        [1.1] => <Double> 1
        [1.2] => <Double> 2
        [2.1] => <Double> 3
        [2.2] => <Double> 4
    )
    
    0 讨论(0)
  • 2020-12-30 01:52

    Try This

    You can also type code1: code2 instead of code1 _
    This is only for displaying
    If arr is defined as array( array(,),array(,)...)
    and Then
    You shoud For c = Lbound(arr(r),1) to ubound(arr(r),1)
    Debug.Print arr(r)(c)
    Because first is 2d Array and the last is 1nd Array.
    I

    'Define 2d array
    arr = [ {"A",1; "B",2; "C",3 } ]: _
    For r = LBound(arr, 1) To UBound(arr, 1): _
            For c = LBound(arr, 2) To UBound(arr, 2): _
                Debug.Print arr(r, c): _
           Next c: _
    Next
    
    0 讨论(0)
  • 2020-12-30 01:55

    paste below data in column A(Name ) in column B (Age)

    Name    Age
    A   10
    B   20
    C   30
    D   40
    E   50
    F   60
    G   70
    

    and run the below code

    Sub Array_Demo()
    
    Dim arr() As String
    Dim i As Integer, j As Integer
    
    ' fill the array with strings
    
    Last = Range("A" & Rows.Count).End(xlUp).Row
    ReDim arr(Last, 1)
    
    For i = 0 To Last - 1
    
        arr(i, 0) = Cells(i + 1, 1).Value
        arr(i, 1) = Cells(i + 1, 2).Value
    
    
    Next
    
    ' only one loop to display its contents !!!
    Dim v As Variant
    
    For Each v In arr
    
        Debug.Print v
    
    Next
    
    
    End Sub
    

    You can see the below output in Immediate window

    Name
    A
    B
    C
    D
    E
    F
    G
    
    Age
    10
    20
    30
    40
    50
    60
    70
    
    0 讨论(0)
提交回复
热议问题