如何用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