Excel VBA,2003-将字符串转换为二维数组

问题描述 投票:0回答:2

我从2005年开始继承了旧的Excel(.xls)电子表格。

出于某种原因,电子表格使用名称管理器来存储以字符串形式存储的大量数据,而不是使用该数据引用表。这是我要更改的内容,因此可以更轻松地更改当前存储在名称管理器中的数据(目前无法更改任何数据,因为已经超出了名称管理器中的字符限制) 。

[我想尽可能多地使用当前的VBA代码,这就是为什么我目前使用VBA将电子表格中的数据转换为[]形式的字符串的原因。

"CELL"\"CELL"\"...""CELL";"CELL"\"CELL"\"CELL";

我(或旧代码)将反斜杠\用作新的collum和分号;的分隔符,作为新行的分隔符。

我想将我的字符串转换为二维数组'arkArray',这样我就可以使用以下代码:

arkCellData = arkArray(i, j)

最佳方法是什么?

我从2005年开始继承了一个旧的Excel(.xls)电子表格。出于某种原因,该电子表格使用名称管理器来存储以字符串形式存储的相当大的数据,而不是使用......来引用表...] >>

< [
这是典型方法:

Sub Fill2D() Dim s As String, r As Range Dim kolumn As Long, roww As Long Dim arr1, arr2, a1, a2 kolumn = 0 roww = 1 s = "alpha\beta\gamma;mike\jim\john;red\blue\green" arr1 = Split(s, ";") For Each a1 In arr1 roww = 1 kolumn = kolumn + 1 arr2 = Split(a1, "\") For Each a2 In arr2 Cells(roww, kolumn) = a2 roww = roww + 1 Next a2 Next a1 End Sub

enter image description here

(您可以更改kolumnroww的起始值以选择不同的起始点)

一旦二维阵列在单元中,您将对其进行验证,然后将其复制到内部VBA阵列中。

如何制作二维数组。

Sub test() Dim myArray(), vS, vS2 Dim vMax() Dim s As String Dim myMax As Integer, i As Integer Dim r As Long s = "apple\banana\John;some\reason\use\Tom;table\data\limit;" If Right(s, 1) = ";" Then s = Left(s, Len(s) - 1) End If vS = Split(s, ";") ReDim vMax(UBound(vS)) For i = 0 To UBound(vS) vS2 = Split(vS(i), "\") vMax(i) = UBound(vS2) + 1 Next i myMax = WorksheetFunction.Max(vMax) r = UBound(vS) + 1 ReDim myArray(1 To r, 1 To myMax) For i = 1 To UBound(myArray) vS2 = Split(vS(i - 1), "\") For j = 1 To UBound(vS2) + 1 myArray(i, j) = vS2(j - 1) Next j Next i Range("a1").Resize(r, myMax) = myArray End Sub

arrays excel vba string
2个回答
0
投票
这是典型方法:

0
投票
如何制作二维数组。

Sub test() Dim myArray(), vS, vS2 Dim vMax() Dim s As String Dim myMax As Integer, i As Integer Dim r As Long s = "apple\banana\John;some\reason\use\Tom;table\data\limit;" If Right(s, 1) = ";" Then s = Left(s, Len(s) - 1) End If vS = Split(s, ";") ReDim vMax(UBound(vS)) For i = 0 To UBound(vS) vS2 = Split(vS(i), "\") vMax(i) = UBound(vS2) + 1 Next i myMax = WorksheetFunction.Max(vMax) r = UBound(vS) + 1 ReDim myArray(1 To r, 1 To myMax) For i = 1 To UBound(myArray) vS2 = Split(vS(i - 1), "\") For j = 1 To UBound(vS2) + 1 myArray(i, j) = vS2(j - 1) Next j Next i Range("a1").Resize(r, myMax) = myArray End Sub

© www.soinside.com 2019 - 2024. All rights reserved.