array(1,2,3)
array(array(1,2,3),array("1","2","3"))
arr(5) '五个元素,下标从0开始
arr(1 to 5) '五个元素,下标从1开始
arr(1 to 5,1 to 10) '二维数组,五行十列
dim arr1()
dim i as integer
i = 10
redim arr(i)
Sub test()
Dim arr
Dim i As Integer
i = 10
ReDim arr(i)
arr(1) = 123
Debug.Print arr(1)
ReDim arr(20)
arr(19) = 188888
Debug.Print arr(19)
End Sub
excel 提示框的启用和禁用:
application.displayalerts = true
Sub test()
Dim arr(1 To 3)
For i = 1 To 3
arr(i) = i * 10
Next i
Sheets(1).Range("a10").Resize(3, 1) = Application.Transpose(arr)
End Sub
Sub test()
Dim arr, arr1
arr = Range("a1:c1")
arr1 = Range("a1:a10")
Stop
End Sub
注意:一维数组的下标默认是从
0
开始的,二维数组的下标默认是从1
开始的
ubound(arr)
索引上界 lbound(arr)
索引下界ubound(arr,1) lbound(arr,1)
行的索引上界和下界,其中的1换成2就是列了Sub test()
Dim arr, arr2()
arr = Array(1, 2, 3)
For i = 0 To UBound(arr):
ReDim Preserve arr2(i)
arr2(i) = arr(i)
Next i
Debug.Print UBound(arr)
End Sub
'二维数组
Sub test()
Dim arr, arr2()
arr = Range("a1:d4")
Dim x, k As Integer
For x = 1 To UBound(arr):
k = k + 1
ReDim Preserve arr2(1 To 4, 1 To k)
arr2(1, k) = arr(x, 1)
arr2(2, k) = arr(x, 2)
arr2(3, k) = arr(x, 3)
arr2(4, k) = arr(x, 4)
Next x
Range("a8").Resize(k, 4) = Application.Transpose(arr2)
End Sub
Sub test()
Dim arr, arr2()
arr = Array(1, 2, 3)
Erase arr
End Sub
Sub test11()
Dim str, arr, allrows, x, x1
allrows = Range("a65535").End(xlUp).Row
arr = Application.Transpose(Range("b1:" & "b" & allrows))
For x = 1 To UBound(arr)
If arr(x) > 100 Then
x1 = x
If x <> UBound(arr) Then
Do
x = x + 1
Loop While arr(x) > 100
str = str & x1 & ":" & x - 1 & ","
Else
str = str & x1 & ":" & x & ","
End If
If Len(str) > 10 Then
Intersect(Range("a:b"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
End If
End If
If x = UBound(arr) Then
Intersect(Range("a:b"), Range(Left(str, Len(str) - 1))).Interior.ColorIndex = 3
End If
Next x
End Sub
application.max|min|large(arr,2)|small
application.sum
application.count
统计数字的个数counta
统计填充的字符数application.match("ab",arr,0)
vba.split
的写法直接使用Sub test()
Dim te
te = "ha-ll-o"
arr = Split(te, "-")
ha = Join(arr, "|")
End Sub
Sub test()
Dim arr, arr1
arr = Range("a1:b1")
arr1 = Filter(Application.Index(arr, 1, 0), "h", True)
End Sub
Sub test()
Dim rg
rg = Range("a1:d4")
arr = Application.Index(rg, 0, 1)
End Sub
Sub test()
Dim arr, arr1
arr = Range("a1:d6")
arr1 = Application.VLookup(Array("h", "h123"), arr, 1, 1)
Stop
End Sub
关于排序的详解介绍参考博文:十大经典排序算法
Sub test()
Dim dict
Set dict = CreateObject("scripting.dictionary")
dict("a") = 12
MsgBox dict("a")
End Sub
Sub test()
Dim dict As New Dictionary
dict("h") = "hello"
Debug.Print dict("h")
End Sub
dict.add cells(1,1).value ,cells(1,2).value
Sub test()
Dim dict As New Dictionary
dict.Add "h", "hello"
dict.Add "h1", "123"
Range("a1").Resize(dict.Count) = Application.Transpose(dict.Keys)
Range("b1").Resize(dict.Count) = Application.Transpose(dict.Items)
End Sub
dict("h") = 1234
dict("h") = "hello"
Sub test()
Dim dict As New Dictionary
dict.Add "h", "hello"
dict.Add "h1", "123"
dict.Remove ("h")
MsgBox dict.Exists("h")
dict.RemoveAll
MsgBox dict.Count
End Sub
Sub test()
Dim dict As New Dictionary
dict.CompareMode = TextCompare
dict.Add "h", "hello"
dict.Add "H", "123"
End Sub
Sub test()
Dim hang As Integer, maxrow As Integer
Dim arr, arr2
Dim arr3 As New Dictionary
maxrow = Range("a65536").End(xlUp).Row
arr = Range("a1:b" & maxrow)
For i = 1 To UBound(arr)
If arr3.Exists(arr(i, 1)) Then
Range("e" & arr3(arr(i, 1))) = Range("e" & arr3(arr(i, 1))) + arr(i, 2)
Else
hang = hang + 1
arr3(arr(i, 1)) = hang
Range("d" & hang) = arr(i, 1)
Range("e" & hang) = arr(i, 2)
End If
Next i
End Sub