新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim arrA() As Variant
Dim eleA As Variant
arrA = Array("Apple", "Banana", "Cherry", 1, 2, 3, Chr(0), Chr(65))
ReDim Preserve arrA(UBound(arrA) + 1) '扩大数组
arrA(UBound(arrA)) = "New" '给最后一个数组赋值
For Each eleA In arrA
Debug.Print TypeName(eleA) & ":" & eleA
Next
End Sub
Private Sub Command2_Click()
Dim varWord() As Variant
Dim arrB() As Byte
Dim i As Integer
'初始化 arrB 数组
varWord = Array(1, 2, 3)
Call s_Array2ArrB(varWord, arrB())
'输出 arrC 数组的内容
For i = 0 To UBound(arrB)
Debug.Print arrB(i)
Next
End Sub
Private Sub s_Array2ArrB(varWord As Variant, arrB() As Byte)
'最后把生成的字节放入arrB()
Dim i As Integer
ReDim arrB(0 To UBound(varWord)) '调整 arrB 数组大小
'将 arrA 的元素转换为 Byte 类型并存储到 arrB 中
For i = 0 To UBound(varWord)
arrB(i) = CByte(varWord(i))
Next
End Sub
运行Command1和Command2的结果:
String:Apple
String:Banana
String:Cherry
Integer:1
Integer:2
Integer:3
String:
String:A
String:New
1
2
3