前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >yhd-ExcelVBA根据条件查找指定文件的数据填写到当前工作表指定列

yhd-ExcelVBA根据条件查找指定文件的数据填写到当前工作表指定列

作者头像
哆哆Excel
发布2022-10-25 13:56:29
1.6K0
发布2022-10-25 13:56:29
举报
文章被收录于专栏:哆哆Excel哆哆Excel

yhd-ExcelVBA根据条件查找指定文件的数据填写到当前工作表指定列

【问题】当我们要用一个表的数据来查询另一个表的数据时,我们常常是打开文件复制数据源表的数据到当前文件新建一个数据表,再用伟大的VLookup,查询过来。再删除掉数据源,这样才完成。

【解决方法】个人感觉这样不够快,所以想了一下方法,设计出如下的东东

【功能与使用】

  1. 设置好要取“数据源”的文件路径
  2. data_key_col = "B" data_item_col = "V"为数据源的key列与item列
  3. this**是当前的数据表的要的东东
代码语言:javascript
复制
Sub getFiledata_to_activesheet()
    Dim mydic As Object, obj As Object, main_sht As Worksheet
    Dim Urng As Range
    Dim arr, brr, temp_rr()
    Set mydic = CreateObject("scripting.dictionary")
    Application.ScreenUpdating = False
    ti = Timer
'==========设定初始数据====================================、
    file = "F:\家Excel学习\yhd-Excel\yhd-Excel-VBA\yhd-ExcelVBA根据条件查找指定文件的数据填写到当前工作表指定列\201908工资变动名册表.xls"
    file_sht = "工资变动名册"
    data_key_col = "B"
    data_item_col = "V" '===要取的数据的列
    this_key_col = "C"
    this_item_col = "AG" '===要输入的当前列
    this_star_n = 5
    this_end_n = 2468
' =========================================
    Set obj = GetObject(file)
    With obj.Worksheets(file_sht)
        brr = .UsedRange.Value
        For i = 1 To UBound(brr)
            s = .Cells(i, data_key_col)
            If s <> "" Then
                mydic(s) = .Cells(i, data_item_col)
            End If
        Next i
        
    End With
    obj.Close False
    Set obj = Nothing
    With ActiveSheet
        For i = this_star_n To this_end_n
            s = .Cells(i, this_key_col)
            If mydic.exists(s) Then
                .Cells(i, this_item_col) = mydic(s)
            Else
'                .Cells(i, this_item_col) = "无"
            End If
        Next i
    End With
    Application.ScreenUpdating = True
    MsgBox "完成!时间为:" & Format(Timer - ti, "0.000秒")
End Sub

完成时间,一个字“快”,比复制与vLookup快很多

====个人学习收藏用的====

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 哆哆Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档