VBA - MsgBox a 2D数组(矩阵)

时间:2021-07-08 21:34:21

I am trying to visualize a 2D Matrix (Array) using a MsgBox, but the code I have doesn't give me the correct representation.

我正在尝试使用MsgBox来可视化一个二维矩阵(数组),但是我的代码并没有给出正确的表示。

Sub test()

Dim M(22, 7) As Double
TwoD_Array_Matrix_In_MSGBOX (M)

End Sub
'_________________________________________________________________________

Public Function TwoD_Array_Matrix_In_MSGBOX(arr As Variant)

h = UBound(arr, 1)
w = UBound(arr, 2)
'MsgBox ("h = " & CStr(h + 1) & vbCrLf & "w = " & CStr(w + 1)) ' to check if the width and hight of the Matrix are correct
Dim msg As String

For i = 0 To w
    For ii = 0 To h
        msg = msg & arr(ii, i) & vbTab
    Next ii
    msg = msg & vbCrLf
Next i

MsgBox msg

End Function

This is the result I get:

这是我得到的结果:

VBA - MsgBox a 2D数组(矩阵)

3 个解决方案

#1


2  

You have w and h interchanged.

w和h交换。

Dim msg As String

For i = 0 To h
    For ii = 0 To w
        msg = msg & arr(i, ii) & vbTab
    Next ii
    msg = msg & vbCrLf
Next i

MsgBox msg

#2


3  

this works perfectly for me

这对我来说再合适不过了

Private Sub this()
    Dim this(22, 7) As Integer
    Dim msg$
    For i = LBound(this, 1) To UBound(this, 1)
        For j = LBound(this, 2) To UBound(this, 2)
            msg = msg & this(i, j) & vbTab
        Next j
    Next i
    MsgBox msg
End Sub

VBA - MsgBox a 2D数组(矩阵)

#3


0  

It might be more flexible to write a function which returns a string, a sort of 2-dimensional join, which allows you to choose both the item delimiter (defaulting to vbTab) and the row delimiter (defaulting to vbCrLf).

编写一个返回字符串(一种二维连接)的函数可能更灵活,它允许您同时选择项目分隔符(默认为vbTab)和行分隔符(默认为vbCrLf)。

You can MsgBox this string -- or write it to the immediate window -- or (with a comma chosen as one of the delimiters) -- write it to a CSV file, etc.:

您可以MsgBox这个字符串—或将它写到当前窗口—或(以逗号作为分隔符之一)—将它写到CSV文件中,等等:

Function MatrixJoin(M As Variant, Optional delim1 As String = vbTab, Optional delim2 As String = vbCrLf) As String
    Dim i As Long, j As Long
    Dim row As Variant, rows As Variant

    ReDim rows(LBound(M, 1) To UBound(M, 1))
    ReDim row(LBound(M, 2) To UBound(M, 2))

    For i = LBound(M, 1) To UBound(M, 1)
        For j = LBound(M, 2) To UBound(M, 2)
            row(j) = M(i, j)
        Next j
        rows(i) = Join(row, delim1)
    Next i
    MatrixJoin = Join(rows, delim2)
End Function

Tested by:

测试通过:

Sub test()
    Dim A As Variant
    A = Range("A1:B3").Value
    MsgBox MatrixJoin(A)
    Debug.Print MatrixJoin(A, ",", ";")
End Sub

Screenshots of output:

输出的截图:

VBA - MsgBox a 2D数组(矩阵)


VBA - MsgBox a 2D数组(矩阵)

#1


2  

You have w and h interchanged.

w和h交换。

Dim msg As String

For i = 0 To h
    For ii = 0 To w
        msg = msg & arr(i, ii) & vbTab
    Next ii
    msg = msg & vbCrLf
Next i

MsgBox msg

#2


3  

this works perfectly for me

这对我来说再合适不过了

Private Sub this()
    Dim this(22, 7) As Integer
    Dim msg$
    For i = LBound(this, 1) To UBound(this, 1)
        For j = LBound(this, 2) To UBound(this, 2)
            msg = msg & this(i, j) & vbTab
        Next j
    Next i
    MsgBox msg
End Sub

VBA - MsgBox a 2D数组(矩阵)

#3


0  

It might be more flexible to write a function which returns a string, a sort of 2-dimensional join, which allows you to choose both the item delimiter (defaulting to vbTab) and the row delimiter (defaulting to vbCrLf).

编写一个返回字符串(一种二维连接)的函数可能更灵活,它允许您同时选择项目分隔符(默认为vbTab)和行分隔符(默认为vbCrLf)。

You can MsgBox this string -- or write it to the immediate window -- or (with a comma chosen as one of the delimiters) -- write it to a CSV file, etc.:

您可以MsgBox这个字符串—或将它写到当前窗口—或(以逗号作为分隔符之一)—将它写到CSV文件中,等等:

Function MatrixJoin(M As Variant, Optional delim1 As String = vbTab, Optional delim2 As String = vbCrLf) As String
    Dim i As Long, j As Long
    Dim row As Variant, rows As Variant

    ReDim rows(LBound(M, 1) To UBound(M, 1))
    ReDim row(LBound(M, 2) To UBound(M, 2))

    For i = LBound(M, 1) To UBound(M, 1)
        For j = LBound(M, 2) To UBound(M, 2)
            row(j) = M(i, j)
        Next j
        rows(i) = Join(row, delim1)
    Next i
    MatrixJoin = Join(rows, delim2)
End Function

Tested by:

测试通过:

Sub test()
    Dim A As Variant
    A = Range("A1:B3").Value
    MsgBox MatrixJoin(A)
    Debug.Print MatrixJoin(A, ",", ";")
End Sub

Screenshots of output:

输出的截图:

VBA - MsgBox a 2D数组(矩阵)


VBA - MsgBox a 2D数组(矩阵)