ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2844|回复: 4

[求助] 不打开excel表的情况下引用数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-3-20 12:55 | 显示全部楼层 |阅读模式
  求助
在不打开表格的情况下提取里面的数据可能吗?
我在每日计划的表格里面输入订单号,后面的简称、数量自动从对应单号的excel表里面提取填入。
见附件  
不胜感激

新建文件夹.rar

41.64 KB, 下载次数: 49

TA的精华主题

TA的得分主题

发表于 2019-3-20 14:23 | 显示全部楼层
供测试

  1. Sub FuYun_chaxun1()
  2. Application.DisplayAlerts = False
  3. Application.ScreenUpdating = False
  4. Dim Mypath As String, Myname As String
  5. Dim Dingdan As String
  6. Dim nRow As Integer
  7. Dim wb As Workbook
  8. ReDim arr(100, 2)

  9. Set dic = CreateObject("scripting.dictionary")

  10. Mypath = ThisWorkbook.Path & ""
  11. Myname = Dir(Mypath & "*.XL*")
  12. n = 0

  13. Do While Myname <> ""
  14.    If Myname <> ThisWorkbook.Name Then
  15.       Set wb = GetObject(Mypath & Myname)
  16.    End If
  17.    
  18.    With wb.Sheets("包才使用记录书")
  19.      arr(n, 0) = .Range("z2").Value
  20.      arr(n, 1) = .Range("f3").Value
  21.      arr(n, 2) = .Range("z4").Value
  22.    End With
  23.    
  24. Myname = Dir()

  25. n = n + 1
  26. Loop

  27. '把数据放入字典

  28. For i = 0 To UBound(arr)
  29.     If arr(i, 0) <> "" Then
  30.            dic(arr(i, 0)) = Array(arr(i, 1), arr(i, 2))
  31.     End If
  32. Next i

  33. nRow = Range("b" & Rows.Count).End(3).Row

  34. For i = 7 To nRow
  35.     Range("b" & i).Offset(0, 1).Resize(1, 2) = dic(Range("b" & i).Value)
  36. Next i


  37. Application.DisplayAlerts = True
  38. Application.ScreenUpdating = True

  39. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2019-3-20 14:24 | 显示全部楼层

如果你的工作表,超过100个 那么把  ReDim arr(100, 2)  前面的那个100  修改成比你工作表大的值,就可以了

TA的精华主题

TA的得分主题

发表于 2019-3-20 23:26 | 显示全部楼层
在每日计划的Sheet1工作表中添加Worksheet_Calculate事件如下:
Private Sub Worksheet_Calculate()
If js = True Then Exit Sub   '当js为true时退出该事件
js = True
s.try Array("tryM"), , Array("FinnallyM")   '在最后需把js设定为false,所以此处使用了try-catch-finnally函数
End Sub

再插入一个模块写入以下代码:
Public js As Boolean   '这个变量用于控制是否执行Worksheet_Calculate事件

Public Function s() As Object  '这一句必须,用于引用SqlCel函数
    Set s = Application.COMAddIns("SqlCelAddIn").Object
End Function

Sub tryM()
On Error GoTo line  '这一句必须用于错误处理
    Dim rn As Range, i As Integer, j As Integer
    If ActiveCell.Column = 3 Then Set rn = ActiveCell.Offset(0, -1)
    If ActiveCell.Column = 2 Then rn = ActiveCell.Offset(0, -1)
    If rn Is Nothing Then Exit Sub
    Dim qax1 As Variant, qax2 As Variant, qax3 As Variant, qaxrow As Variant
    Set qax1 = s.exceltoqax(ThisWorkbook.path & "\20002394.xls", "半成品出入库传票$E4:W17")  '将20002394.xls的部分数据读入qax1数据集
    Set qax2 = s.exceltoqax(ThisWorkbook.path & "\20002365.xls", "半成品出入库传票$E4:W17")  '将20002365.xls的部分数据读入qax2数据集
   
    '*****************此处循环用于将qax2数据集的数据合并到qax1中
    For i = 0 To s.qaxrows(qax2) - 1
        Set qaxrow = s.newrow(qax1)
        For j = 0 To s.qaxcols(qax1) - 1
            Set qaxrow = s.setrowcell(qaxrow, j, s.getcell(qax2, i, j))
        Next j
        Set qax1 = s.addrow(qax1, qaxrow)
    Next i
   
    Set qax3 = s.qaxselect(qax1, "产品CODE='" & rn.Value & "'")   '筛选qax1数据集
    If s.qaxrows(qax2) > 0 Then
        rn.Offset(0, 1).Value = s.getcell(qax2, 0, 6)  '从数据集中取出数据并赋值给单元格
        rn.Offset(0, 3).Value = s.getcell(qax2, 0, 18)
    End If
    Exit Sub
line:  '这一句必须用于错误处理
    s.setErr Err  '这一句必须用于错误处理
End Sub

Sub FinnallyM() '这一句非必须用于最终将js设定为false
js = False
End Sub

以上代码需安装sqlcelfuncs插件才能正常运行。

TA的精华主题

TA的得分主题

发表于 2019-3-20 23:34 | 显示全部楼层
其中TryM应该这么写
Sub tryM()
On Error GoTo line  '这一句必须用于错误处理
    Dim rn As Range, i As Integer, j As Integer
    If ActiveCell.Column = 3 Then Set rn = ActiveCell.Offset(0, -1)
    If ActiveCell.Column = 2 Then Set rn = ActiveCell.Offset(-1, 0)
    If rn Is Nothing Then Exit Sub
    Dim qax1 As Variant, qax2 As Variant, qax3 As Variant, qaxrow As Variant
    Set qax1 = s.exceltoqax(ThisWorkbook.path & "\20002394.xls", "半成品出入库传票$E4:W17")  '将20002394.xls的部分数据读入qax1数据集
    Set qax2 = s.exceltoqax(ThisWorkbook.path & "\20002365.xls", "半成品出入库传票$E4:W17")  '将20002365.xls的部分数据读入qax2数据集
   
    '*****************此处循环用于将qax2数据集的数据合并到qax1中
    For i = 0 To s.qaxrows(qax2) - 1
        Set qaxrow = s.newrow(qax1)
        For j = 0 To s.qaxcols(qax1) - 1
            Set qaxrow = s.setrowcell(qaxrow, j, s.getcell(qax2, i, j))
        Next j
        Set qax1 = s.addrow(qax1, qaxrow)
    Next i
   
    Set qax3 = s.qaxselect(qax1, "产品CODE='" & rn.Value & "'")   '筛选qax1数据集
    If s.qaxrows(qax2) > 0 Then
        rn.Offset(0, 1).Value = s.getcell(qax2, 0, 6)  '从数据集中取出数据并赋值给单元格
        rn.Offset(0, 3).Value = s.getcell(qax2, 0, 18)
    End If
    Exit Sub
line:  '这一句必须用于错误处理
    s.setErr Err  '这一句必须用于错误处理
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 15:55 , Processed in 0.040607 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表