您的位置:

深入探究vbavlookup函数

一、vbadir函数

vbadir函数可以返回目录中的文件名和目录名的列表,我们可以用vbadir函数来列举出所有的文件名和目录名,然后再通过vbavlookup函数在这个列表中查找某个特定的文件名或目录名。


Dim fileList() As String
Dim fileName As String
Dim i As Integer
fileList = VBA.FileSystem.GetFiles("C:\Test\")
For i = LBound(fileList) To UBound(fileList)
    fileName = VBA.Strings.Mid(fileList(i), VBA.Strings.InStrRev(fileList(i), "\") + 1, 256)
    Debug.Print fileName
Next i

二、vbarank函数

vbarank函数可以返回指定数值在一个数组中的排名,我们可以用vbarank函数来进行排名,然后再通过vbavlookup函数在这个排名列表中查找某个特定数值的位置。


Dim testArray(10) As Integer
Dim pos As Integer
Dim num As Integer
testArray(1) = 3
testArray(2) = 1
testArray(3) = 5
testArray(4) = 9
testArray(5) = 2
testArray(6) = 7
testArray(7) = 6
testArray(8) = 4
testArray(9) = 8
testArray(10) = 10
pos = VBA.Application.WorksheetFunction.Rank(7, testArray, 1)
Debug.Print pos

三、vbarray函数

vbarray函数可以将一个一维或二维数组转化为一个分隔的字符串,我们可以用vbarray函数来将数组变成一个字符串,然后再通过vbavlookup函数在这个字符串中查找某个特定元素的位置。


Dim testArray(3, 3) As Integer
Dim str As String
Dim pos As Integer
testArray(0, 0) = 1
testArray(0, 1) = 6
testArray(0, 2) = 3
testArray(1, 0) = 2
testArray(1, 1) = 8
testArray(1, 2) = 5
testArray(2, 0) = 4
testArray(2, 1) = 9
testArray(2, 2) = 7
str = VBA.Strings.Join(VBA.Array(testArray), ",")
pos = VBA.Strings.InStr(1, str, "8", vbTextCompare)
Debug.Print pos

四、vbahlookup函数

vbahlookup函数可以在一个二维数组中查找某个特定值,我们可以将数据放到二维数组中,然后通过vbahlookup函数查找某个特定值所在的行号和列号。


Dim testArray(3, 3) As Integer
Dim RowPos As Integer
Dim ColPos As Integer
testArray(0, 0) = 1
testArray(0, 1) = 6
testArray(0, 2) = 3
testArray(1, 0) = 2
testArray(1, 1) = 8
testArray(1, 2) = 5
testArray(2, 0) = 4
testArray(2, 1) = 9
testArray(2, 2) = 7
VBA.Application.WorksheetFunction.VLookup(8, testArray, 2, False)
VBA.Application.WorksheetFunction.HLookup(8, testArray, 1, False)

五、vbatrim函数

vbatrim函数可以删除一个字符串中的前导和尾随空格,我们可以用vbatrim函数来清理一些字符串,然后再通过vbavlookup函数在这些字符串中查找某个特定值的位置。


Dim testString As String
Dim pos As Integer
testString = " hello, world! "
testString = VBA.Strings.Trim(testString)
pos = VBA.Strings.InStr(1, testString, "world", vbTextCompare)
Debug.Print pos

六、excelvlookup函数

excelvlookup函数是Excel中的一种查找函数,可以在一个二维的表格中查找某个特定值,我们可以在Excel中使用excelvlookup函数进行查找,然后再将结果返回到VBA代码中。


Dim rng As Range
Set rng = Range("A1:B10")
result = VBA.Application.WorksheetFunction.VLookup("Banana", rng, 2, False)

七、vlookup函数excel

vlookup函数是Excel中的一种查找函数,与excelvlookup函数相似,可以在一个二维的表格中查找某个特定值,但是写法略微不同。


Dim rng As Range
Set rng = Range("A1:B10")
result = VBA.Application.WorksheetFunction.vlookup("Banana", rng, 2, False)

以上就是对vbavlookup函数的多方位探究,希望对你有所帮助。