将字符串拆分为矩阵vba

将字符串拆分为矩阵vba,vba,matrix,split,Vba,Matrix,Split,我在一行上有3个信息,我可以选择多行。所以我要寻找的是一种方法,第一次将每一行分割成一个数组 这就是我在这里要做的。 行=拆分(消息“,”) 然后我想为每一行分割信息,得到一个矩阵,第一个标识行,第二个标识信息 ReDim pro(Ubound(line),3) For i = 0 To Ubound(line) pro(i) = Split(ligne(i), "/") Next 但它会给我一个不匹配的错误,所以我不知道怎么做 例如: 我有这个 msg1/1250/说明,msg2/1500/

我在一行上有3个信息,我可以选择多行。所以我要寻找的是一种方法,第一次将每一行分割成一个数组

这就是我在这里要做的。 行=拆分(消息“,”)

然后我想为每一行分割信息,得到一个矩阵,第一个标识行,第二个标识信息

ReDim pro(Ubound(line),3)
For i = 0 To Ubound(line)
pro(i) = Split(ligne(i), "/")
Next
但它会给我一个不匹配的错误,所以我不知道怎么做

例如:

我有这个

msg1/1250/说明,msg2/1500/说明2,MSG345656,说明3

最后,我要说:

pro(0,0)=msg1

pro(0,1)=1250

pro(1,1)=1500 等等


谢谢你

无论如何都不是最理想的,但它应该给你一个开始:

Dim RowCount As Integer
Dim i As Integer
Dim j As Integer
Dim x As Variant
Dim y As Variant
Line = "msg1/1250/Description,msg2/1500/Description2,msg3/45656/Desctiption3"
RowCount = UBound(Split(Line, ",")) + 1
ReDim pro(RowCount, 3)
For Each x In Split(Line, ",")
    j = 0
    For Each y In Split(x, "/")
        pro(i, j) = y
        j = j + 1
    Next y
    i = i + 1
Next x

您最初使用的
pro
称为“锯齿阵列”。可以使用“双转置”将其转换为二维数组。但请注意,它需要所有“线阵列”具有相同的大小:

Function toMatrix(msg as string)
  Dim line: line = Split(msg, ",")
  ReDim pro(UBound(line))
  Dim i As Long
  For i = 0 To UBound(line)
    pro(i) = Split(line(i), "/")
  Next

  ' transform array of arrays into a 2D array.  
  toMatrix = Application.Transpose(Application.Transpose(pro))
End Function

Sub Test
  Dim msg As String
  msg = "msg1/1250/Description,msg2/1500/Description2,msg3/45656/Desctiption3"
  Dim ar
  ar = toMatrix(msg) ' ar is now a 2D array
End Sub
我就是这样做的:

Option Explicit

Public Sub TestMe()

    Dim strInput            As String

    Dim arrVals             As Variant
    Dim arrVar              As Variant
    Dim arrVar2             As Variant
    Dim arrResult           As Variant
    Dim lngCount            As Long: lngCount = 0

    strInput = "msg1/1250/Description,msg2/1500/Description2,msg3/45656/Desctiption3"
    arrVals = Split(strInput, ",")

    ReDim arrResult(UBound(arrVals), 1)

    For Each arrVar In arrVals

        arrVar2 = Split(arrVar, "/")
        arrResult(lngCount, 0) = arrVar2(0)
        arrResult(lngCount, 1) = arrVar2(1)
        lngCount = lngCount + 1

    Next arrVar

End Sub
这就是结果:


就我所知,您不需要
说明n
我跳过了它。

您能给出更多的例子吗?如果您删除
+1
并将
ReDim-pro(RowCount,3)
更改为
ReDim-pro(RowCount,2)
,您的解决方案会更好。当前,矩阵中的行和列中都有空值。但仍然足够好:)谢谢,我不认为它与我的数组类型有关,我甚至不知道你可以转置数组来转换它们谢谢你,我想我会在我的程序中以你为例,我真的很感激