「代码」代替VLOOKUP函数,用Application.Match怎么样?结果却大大出乎我的意料!

文摘   教育   2024-10-25 23:59   江苏  

点【关于本公众号】了解一下,欢迎关注谢谢!

快速浏览

实用案例

|日期控件||简单的收发存||收费管理系(Access改进版)|

|电子发票管理助手||电子发票登记系统(Access版)|

|文件合并||表格拆分||审计凭证抽查底稿|

|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|

|印章使用登记系统|

收费使用项目

|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|

内容提要

  • 字典代替VLOOKUP|完整代码
1、在工作表“查询”里,命令按钮点击事件,工作表Change事件,调用相应过程:
Private Sub CmdQuery_Click()    Call myQueryEnd Sub
Private Sub CmdQuery2_Click() Call myQuery2End Sub
Private Sub CmdQuery3_Click() Call myQuery3End Sub
Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Long, currRow As Long Dim rowField, colField Dim arr() If Target.CountLarge > 1 Then Exit Sub If Target.Column = 1 And Target.row > 1 Then arr = ThisWorkbook.Sheets("Ö°¹¤µµ°¸").UsedRange.Value currRow = Target.row rowField = Target.Value For i = 2 To UsedRange.Columns.Count colField = Cells(1, i) If colField <> "" Then Cells(currRow, i) = myMatch(arr, rowField, colField) End If Next End IfEnd Sub
2、在myModule里,myQuery、myQuery2过程、myMatch自定义函数,查询数据:
Sub myQuery()    Dim i As Long, j As Long    Dim ws As Worksheet, rng As Range    Dim lRow As Long, lCol As Integer    Dim arr(), currRow As Long, temp()    Dim t As Double    t = Timer    Set ws = ThisWorkbook.Sheets("职工档案")    With ws        lRow = .Cells(.Rows.Count, 1).End(xlUp).row        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column        arr = .Cells(1, 1).Resize(lRow, lCol).Value    End With    Set ws = ThisWorkbook.Sheets("查询")    With ws        lRow = .Cells(.Rows.Count, 1).End(xlUp).row        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column        Set rng = .Cells(1, 1).Resize(lRow, lCol)        temp = rng.Value    End With    For i = 2 To lRow            Debug.Print Timer - t
For j = 2 To lCol temp(i, j) = myMatch(arr, temp(i, 1), temp(1, j)) Next Next With rng .Cells.NumberFormat = "@" .Columns(3).NumberFormat = "0" .Value = temp End With MsgBox "Done! Time consumed: " & Round(Timer - t, 2) & " seconds." End Sub
Sub myQuery2() Dim i As Long, j As Long Dim ws As Worksheet, rng As Range Dim lRow As Long, lCol As Integer Dim arr(), currRow As Long, currCol As Long, temp() Dim t As Double t = Timer Set ws = ThisWorkbook.Sheets("职工档案") With ws lRow = .Cells(.Rows.Count, 1).End(xlUp).row lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column arr = .Cells(1, 1).Resize(lRow, lCol).Value End With Set ws = ThisWorkbook.Sheets("查询") With ws lRow = .Cells(.Rows.Count, 1).End(xlUp).row lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column Set rng = .Cells(1, 1).Resize(lRow, lCol) temp = rng.Value End With For i = 2 To lRow currRow = Application.Match(temp(i, 1), Application.Index(arr, 0, 1), 0) Debug.Print Timer - t temp(i, 2) = arr(currRow, 2) temp(i, 3) = arr(currRow, 6) temp(i, 4) = arr(currRow, 9) temp(i, 5) = arr(currRow, 11) Next With rng .Cells.NumberFormat = "@" .Columns(3).NumberFormat = "0" .Value = temp End With MsgBox "Done! Time consumed: " & Round(Timer - t, 2) & " seconds."End Sub
Function myMatch(arr(), rowField, colField, Optional matchCol As Long = 1) On Error Resume Next Dim row(), col() Dim iRow As Long, iCol As Long row = Application.Index(arr, 0, matchCol) col = Application.Index(arr, 1) iRow = Application.Match(rowField, row, 0) iCol = Application.Match(colField, col, 0) myMatch = arr(iRow, iCol)End Function
3、在myModule里,myQuery3,使用字典的方法查询数据
Sub myQuery3()    '//采用字典    Dim i As Long, j As Long    Dim ws As Worksheet, rng As Range    Dim lRow As Long, lCol As Integer    Dim arr(), currRow As Long, currCol As Long, temp()    Dim dic As Object    Dim t As Double    Set dic = CreateObject("Scripting.Dictionary")    t = Timer    Set ws = ThisWorkbook.Sheets("职工档案")    With ws        lRow = .Cells(.Rows.Count, 1).End(xlUp).row        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column        arr = .Cells(1, 1).Resize(lRow, lCol).Value    End With        '把需要的数据装入字典    For i = 2 To lRow        dic(arr(i, 1)) = Array(arr(i, 2), arr(i, 6), arr(i, 9), arr(i, 11))    Next        Set ws = ThisWorkbook.Sheets("查询")    With ws        lRow = .Cells(.Rows.Count, 1).End(xlUp).row        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column        Set rng = .Cells(1, 1).Resize(lRow, lCol)        temp = rng.Value    End With    For i = 2 To lRow        arr = dic(temp(i, 1))        Debug.Print Timer - t        For j = 2 To lCol            temp(i, j) = arr(j - 2)        Next    Next    With rng        .Cells.NumberFormat = "@"        .Columns(3).NumberFormat = "0"        .Value = temp    End With    MsgBox "Done! Time consumed: " & Round(Timer - t, 2) & " seconds."End Sub
~~~~~~End~~~~~~

安利小店
安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精洗衣液也是日常必备,用过都说好!

合谷医疗
合谷医疗专攻各种疑难杂症,尤其擅长抑郁症焦虑失眠儿童神经发育异常多动症自闭孤独症腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了

喜欢就点个、点在看留言评论、分享一下呗!感谢支持!

  • Excel问题,请在文章下面留言讨论!或者加入我的付费交流群提问

  • 如需案例文件,请按当天另一篇文章末尾案例文件分享说明操作!

VBA编程实战
Excel应用案例、Excel VBA、公式函数使用技巧分享,思路解读...... 这里有鲜活案例、实用的技巧......
 最新文章