vba 二维数组筛选
时间: 2023-12-30 17:01:58 浏览: 191
在VBA中,可以使用循环和条件语句来筛选二维数组。下面是一个示例代码,演示了如何筛选一个二维数组中含有特定字符的元素,并将符合条件的元素组成一个新的一维数组。
```vba
Sub FilterArray()
Dim arr(1 To 3, 1 To 2) As String
Dim filteredArr() As String
Dim i As Integer, j As Integer, k As Integer
' 初始化二维数组
arr(1, 1) = "ABC"
arr(1, 2) = "A"
arr(2, 1) = "CD"
arr(2, 2) = "ABOUT"
arr(3, 1) = "AND"
arr(3, 2) = "END"
' 遍历二维数组,筛选含有"A"的元素
k = 1
For i = 1 To UBound(arr, 1)
For j = 1 To UBound(arr, 2)
If InStr(arr(i, j), "A") > 0 Then
ReDim Preserve filteredArr(1 To k)
filteredArr(k) = arr(i, j)
k = k + 1
End If
Next j
Next i
' 输出筛选结果
For i = 1 To UBound(filteredArr)
Debug.Print filteredArr(i)
Next i
End Sub
```
运行上述代码,将会输出符合条件的元素组成的新数组。在这个例子中,输出结果为:
```
ABC
A
ABOUT
AND
```
阅读全文