vba 如何在VBA的立即窗口中打印二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14274949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:43:36  来源:igfitidea点击:

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

arraysvbaimmediate-window

提问by Qbik

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 Immediate window could solve this problem, because then only thing to do is to loop this code for each line of array.

如何在 VBA 的立即窗口中打印二维数组?是否存在执行此操作的任何通用方法?在立即窗口中每行绘制一行数组的一些方法可以解决这个问题,因为唯一要做的就是为每一行数组循环此代码。

采纳答案by user3706920

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

回答by johny why

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.

为了在程序执行期间进行调试,您需要一种可以在所有 VB 项目中使用的无代码方法来监视数组的内容。

  1. In VBA IDE, click View menu > Locals Window
  2. In Local pane, find the array-name. enter image description here
  3. Expand the nodes to find your values. The nodes will differ, depending on the type of array.
  1. 在 VBA IDE 中,单击“查看”菜单 >“本地窗口”
  2. 在本地窗格中,找到数组名称。 在此处输入图片说明
  3. 展开节点以找到您的值。节点会有所不同,具体取决于数组的类型。

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

在这个例子中,“aLookList”是一个变体数组。“Value2”下的值来自一个范围。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

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.

此处的另一个答案建议使用 Watch 窗格。这与我的回答相似,但海报没有解释您可以通过简单地将数组名称添加到 Watch 来监视整个数组(所有单元格)。然后,深入节点。Locals Window 相对于 Watch Window 的优势在于,有了 Locals 窗格,您不必手动将数组添加到窗格中,它已经存在了。所以它的努力要少一些。

回答by Alex K.

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.
  • 创建并调用一个循环并将其打印到调试窗口的函数。
  • 如果这是为了调试,请右键单击 IDE 中的变量并“添加监视”,这将打开一个窗口,该窗口将跟踪数组值的更改并在遇到断点时显示其内容。

回答by Swapnil

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

将 A 列(名称)中的数据粘贴到 B 列(年龄)中

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

回答by user3235365

Try This

尝试这个

您也可以键入 code1: code2 而不是 code1 _


这仅用于显示


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)


因为第一个是二维数组,最后一个是第一个数组。


一世

'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

回答by Yaslaw

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.

我为此编写了自己的函数。用于 VBA 的 print_r()。它可以评估数组、字典、集合、匹配集合等。整体也嵌套。此外,还给出了数据类型,并将特殊字符以 Unicode 格式显示在字符串中。

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

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
)