如何用VBA實現vlookup的功能


答:如果編寫一個自定義函數,可能速度會慢一點。如何是實現功能,可以利用字典對象來模擬實現,速度可能會快一點。以下是我寫的2段代碼e69da5e6ba90e79fa5e9819331333365653864,供參考。

1
2
3
4
5
6
7
8
9
10
11
12
Function  MyVlookup(LookValue  As  Range, ArrRng  As  Range, Ofst  As  Integer )
     Dim  RltRng  As  Range
     Dim  AcLookValue  As  Range, AcArrRng  As  Range
     Set  AcLookValue = LookValue.Cells(1, 1)
     Set  AcArrRng = Intersect(ArrRng.Parent.UsedRange, ArrRng)
     On  Error  Resume  Next
     Set  RltRng = AcArrRng.Columns(1).Find(what:=AcLookValue, lookat:=xlPart)
     On  Error  GoTo  0
     If  Not  RltRng  Is  Nothing  Then
         MyVlookup = RltRng.Offset(0, Ofst - 1)
     End  If
End  Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sub  Demo()
     Dim  Dic  As  Object
     Dim  Cell  As  Range
     Set  Dic = CreateObject( "scripting.dictionary" )
     For  Each  Cell  In  Range( "G2:G12" )
         Dic.Add Cell.Value, Cell.Offset(0, 1).Value
     Next
 
     For  Each  Cell  In  Range( "D2:D12" )
         If  Dic.exists(Cell.Offset(0, -3).Value)  Then
             Cell = Dic.Item(Cell.Offset(0, -3).Value)
         End  If
     Next
End  Sub


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM