我有一个这样的数组
dim arr(1 to 5) as string
arr(1)="a"
arr(3)="b"
arr(5) = "c"
Run Code Online (Sandbox Code Playgroud)
(arr(2),arr(4) 为空)。
我如何重新调整这个 arr(1to5) 以排除空值并保存值“a”、“b”、“c”(我想要像 那样的输出arr(1to3), arr(1)="a", arr(2)="b", arr(3)="c")?一般来说,我不知道其中有多少是空的,所以我需要一些通用代码(不是针对这个特定示例)。
我正在考虑使用新的临时数组来保存所有非空值,然后 redim arr(1to5)。
也许这是一种更好(快速)的方式来做到这一点?
I wrote sth similar:
Sub test()
Dim myArray() As String
Dim i As Long
Dim y As Long
ReDim Preserve myArray(3)
myArray(1) = "a"
myArray(3) = "c"
Dim myArray2() As String
y = 1
For i = LBound(myArray) To UBound(myArray)
If myArray(i) <> "" Then
ReDim Preserve myArray2(y)
myArray2(y) = myArray(i)
y = …Run Code Online (Sandbox Code Playgroud)