如何在VBA中用字符串填充数组并获得它的长度?
例如,两个单元格可能包含以下信息:
A1:"测试1" A2:"测试2"
Dim example As String
Dim arreglito() As String
example = Range("A2").Value
arreglito(0) = example
example= Range("A1").Value
arreglito(1)= example
MsgBox arreglito(0)
下标超出范围
Dim example As String
Dim arreglito() As Variant
example = Range("A2").Value
arreglito(0) = example
MsgBox arreglito(0)
下标超出范围
答案 0 :(得分:1)
这是一种从工作表到字符串数组添加单个列范围的方法(转置可能有一些大小限制.2 ^ 16是吗?)。
使用Flephal之前的一行来一次性将范围放入字符串数组中。
Sub AddToArray()
Dim arreglito() As String
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("MySheet") 'change as appropriate
Dim srcRange As Range
Set srcRange = ws.Range("A1:A3")
arreglito = Split(Join(Application.Transpose(srcRange), "#"), "#")
MsgBox UBound(arreglito) + 1
End Sub
通过变量数组进行多个列传输:
Sub AddToArray2()
Dim arreglito() As String
Dim sourceArr()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("MySheet") 'change as appropriate
Dim srcRange As Range
sourceArr = ws.Range("A1:C3")
ReDim arreglito(1 To UBound(sourceArr, 1), 1 To UBound(sourceArr, 2))
Dim x As Long
Dim y As Long
For x = LBound(sourceArr, 1) To UBound(sourceArr, 1)
For y = LBound(sourceArr, 2) To UBound(sourceArr, 2)
arreglito(x, y) = CStr(sourceArr(x, y))
Next y
Next x
MsgBox UBound(arreglito, 1) & " x " & UBound(arreglito, 2)
End Sub
答案 1 :(得分:1)
你可以读取整个excel范围到数组,它比逐个单元格读取数据要快得多。
android:layout_width="wrap_content"
请注意,这将始终创建尺寸为1到y,1到x的多维数组(即使只有1列)