函數(shù)可以應(yīng)用于數(shù)組,不需要進行循環(huán)。
一 函數(shù)在數(shù)組中的應(yīng)用
1.1 數(shù)組的最值
Sub s()
Dim arr1()
arr1 = Array(1, 12, 4, 5, 19)
MsgBox "1,12,4,5,19最大值" & Application.Max(arr1)
MsgBox "1,12,4,5,19最小值" & Application.Min(arr1)
MsgBox "1,12,4,5,19第二大值" & Application.Large(arr1, 2)
End Sub
1.2 求和
MsgBox "1,12,4,5,19的和" & Application.Sum(arr1)
application.sum(數(shù)組)
1.3 統(tǒng)計個數(shù)
在工作表中中,統(tǒng)計個數(shù)用countif函數(shù),這個函數(shù)的第一個參數(shù)是單元格引用,所以countif無法對數(shù)組引用。對于數(shù)組,利用counta(已經(jīng)填充內(nèi)容) 和count(數(shù)字個數(shù))函數(shù)統(tǒng)計VBA數(shù)組的數(shù)字個數(shù)以及所有已經(jīng)填充內(nèi)容的個數(shù)。
Sub s1()
Dim arr1, arr2(0 To 10), x
arr1 = Array("a", "3", "", 4, 6)
For x = 0 To 4
arr2(x) = arr1(x)
Next x
MsgBox "數(shù)組1的數(shù)字個數(shù)" & Application.Count(arr1) '結(jié)果是2
MsgBox "數(shù)組1的填充數(shù)值個數(shù)" & Application.CountA(arr1) '結(jié)果是5
MsgBox "數(shù)組2的填充數(shù)值個數(shù)" & Application.CountA(arr2) '結(jié)果是11
MsgBox "數(shù)組2的填充數(shù)字個數(shù)" & Application.Count(arr2) '結(jié)果是2
End Sub
1.4在數(shù)組里查找
在工作表中利用match函數(shù),數(shù)組中也可以用match函數(shù)。
Sub s2()
Dim arr
On Error Resume Next
arr = Array("a", "c", "b", "f", "d")
MsgBox Application.Match("f", arr, 0)
If Err.Number = 13 Then
MsgBox "not find"
End If
End Sub
On Error Resume Next的意思就是略過錯誤
If Err.Number = 13就是說執(zhí)行以上語句以后有沒有錯誤13(類型不匹配)生成(如果執(zhí)行一個語句產(chǎn)生錯誤時會自動設(shè)置Err對象的Number和Description等等屬性)
二 可以生成數(shù)組的函數(shù)
2.1 split,join函數(shù)
Sub s3()
Dim sr, arr
sr = "A-BC-FGR-H"
arr = VBA.Split(sr, "-")
MsgBox arr(2)
End Sub
JOIN 函數(shù),把拆分后的字符串用逗號連接起來。
Sub s3()
Dim sr, arr
sr = "A-BC-FGR-H"
arr = VBA.Split(sr, "-")
MsgBox Join(arr, ",")
End Sub
2.2 filter 函數(shù)
按條件篩選符合條件的值組成一個新的數(shù)組。filter只能進行模糊篩選,是一種包含關(guān)系的篩選,沒有辦法做到精確篩選,若想精確篩選只含有W的,只能用循環(huán)了。
filter(數(shù)組,篩選條件,是/否),如果是,則返回包含的數(shù)組,如果否則返回非包含的數(shù)組。
Sub s4()
Dim arr, arr1, arr2
arr = Application.Transpose(Range("a2:a10")) ‘列化為行,將九行一列數(shù)組化為一維數(shù)組
arr1 = VBA.Filter(arr, "W", True)
arr2 = VBA.Filter(arr, "W", flase)
Range("b2").Resize(UBound(arr1) + 1) = Application.Transpose(arr1) ’將一維數(shù)組化為一列數(shù)據(jù)進行填充
Range("c2").Resize(UBound(arr2) + 1) = Application.Transpose(arr2)
End Sub
2.3 index函數(shù)
index函數(shù)可以把二維數(shù)組的某一列或某一行截取出來,構(gòu)成一個新的數(shù)組。
application.index(二維數(shù)組,0,列數(shù)) 返回二維數(shù)組
application.index(二維數(shù)組,行數(shù),0) 返回一維數(shù)組
若是想截取第幾行到第幾行,只能用循環(huán)解決了。
Sub s5()
Dim arr, arr1, arr2
arr = Range("a2:d6")
arr1 = Application.Index(arr, , 1)
arr2 = Application.Index(arr, 4, 0)
Stop
End Sub
2.4 vlookup函數(shù)
vlookup函數(shù)的第一個參數(shù)可以用VBA數(shù)組,返回的也是一個VBA數(shù)組。
Sub s6()
Dim arr, arr1
arr = Range("a2:d6")
arr1 = Application.VLookup(Array("b", "c"), arr, 4, 0) '先構(gòu)建一個需要查找的內(nèi)容的一維數(shù)組
Stop
End Sub
關(guān)于Vlookup 函數(shù)
Application.VLOOKUP(lookup_value, table_array, column_index, range_lookup)
http://www.exceltrick.com/formulas_macros/vlookup-in-vba/
2.5 sumif 和countif函數(shù)
countif和sumif函數(shù)的第二個參數(shù)都可以使用數(shù)組,所以也可以返回一個VBA 數(shù)組。
Sub s7()
Dim T
T = Timer
Dim arr
arr = Application.SumIf(Range("a2:a10000"), Array("B", "C", "G", "R"), Range("B2:B10000"))???? '判斷區(qū)域,條件區(qū)域,計算區(qū)域
MsgBox Timer - T
End Sub
counif函數(shù)同理。