ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享]不打开文件提取数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-3-23 18:24 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:文件操作和FSO

A VBA Function to Get a Value From a Closed File
VBA does not include a method to retrieve a value from a closed file. You can, however, take advantage of Excel's ability to work with linked files.

This tip contains a VBA function that retrieves a value from a closed workbook. It does by calling an XLM macro.

Note:
You cannot use this function in a worksheet formula.

The GetValue Function
The GetValue function, listed below takes four arguments:

path:  The drive and path to the closed file (e.g., "d:\files")
file:  The workbook name (e.g., "99budget.xls")
sheet: The worksheet name (e.g., "Sheet1")
ref:   The cell reference (e.g., "C4")
Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String

'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If

'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)

'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
End Function

Using the GetValue Function
To use this function, copy the listing to a VBA module. Then, call the function with the appropriate arguments. The Sub procedure below demonstrates. It simply displays the value in cell A1 in Sheet1 of a file named 99Budget.xls, located in the   XLFiles\Budget directory on drive C:.

Sub TestGetValue()
    p = "c:\XLFiles\Budget"
    f = "99Budget.xls"
    s = "Sheet1"
    a = "A1"
    MsgBox GetValue(p, f, s, a)
End Sub
Another example is shown below. This procedure reads 1,200 values (100 rows and 12 columns) from a closed file, and places the values into the active worksheet.

Sub TestGetValue2()
    p = "c:\XLFiles\Budget"
    f = "99Budget.xls"
    s = "Sheet1"
    Application.ScreenUpdating = False
    For r = 1 To 100
        For c = 1 To 12
            a = Cells(r, c).Address
            Cells(r, c) = GetValue(p, f, s, a)
        Next c
    Next r
    Application.ScreenUpdating = True
End Sub
Caveat
In order for this function to work properly, a worksheet must be active in Excel. It will generate an error if all windows are hidden, or if the active sheet is a Chart sheet

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2007-3-23 18:52 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

楼主解释一下吧.

TA的精华主题

TA的得分主题

发表于 2007-3-23 19:00 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-3-23 19:52 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
很感兴趣的代码,希望论坛上e文好的给翻译一下,然后作成范例供大家学习!

TA的精华主题

TA的得分主题

发表于 2007-3-23 20:27 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
QUOTE:
以下是引用zhaodx在2007-3-23 18:24:05的发言:

A VBA Function to Get a Value From a Closed File
VBA does not include a method to retrieve a value from a closed file. You can, however, take advantage of Excel's ability to work with linked files.

This tip contains a VBA function that retrieves a value from a closed workbook. It does by calling an XLM macro.

VBA提供从关闭的工作簿文件中检索值的方法。然而,可以利用EXCEL的能力来处理链接的文件。这一节包含的VBA函数(GETVALUE)可以关闭的工作簿中检索值,如下所示。其中调用了一个XLM宏,这是EXCEL5以前的版本中使用的老式宏。

Note:
You cannot use this function in a worksheet formula.

The GetValue Function
The GetValue function, listed below takes four arguments:

该函数接受四个参数

path:  The drive and path to the closed file (e.g., "d:\files") 已经关闭的文件所在的驱动器盘符和路径
file:  The workbook name (e.g., "99budget.xls") 工作簿名称
sheet: The worksheet name (e.g., "Sheet1") 工作表名称
ref:   The cell reference (e.g., "C4") 单元格引用


Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String

'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If

'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)

'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
End Function

Using the GetValue Function
To use this function, copy the listing to a VBA module. Then, call the function with the appropriate arguments. The Sub procedure below demonstrates. It simply displays the value in cell A1 in Sheet1 of a file named 99Budget.xls, located in the   XLFiles\Budget directory on drive C:.

下面的SUB过程了如何使用GETVALUE函数。其中显示了99Budget.xls文件中SHEET1工作表的单元格A1中的值,该文件位于C盘 XLFiles\Budget 目录下。

Sub TestGetValue()
    p = "c:\XLFiles\Budget"
    f = "99Budget.xls"
    s = "Sheet1"
    a = "A1"
    MsgBox GetValue(p, f, s, a)
End Sub
Another example is shown below. This procedure reads 1,200 values (100 rows and 12 columns) from a closed file, and places the values into the active worksheet.

下面是另一个例子,这个过程从已经关闭的文件中读取了1200个值(100行、12列),然后把这些值放在了活动工作表中。

Sub TestGetValue2()
    p = "c:\XLFiles\Budget"
    f = "99Budget.xls"
    s = "Sheet1"
    Application.ScreenUpdating = False
    For r = 1 To 100
        For c = 1 To 12
            a = Cells(r, c).Address
            Cells(r, c) = GetValue(p, f, s, a)
        Next c
    Next r
    Application.ScreenUpdating = True
End Sub
Caveat
In order for this function to work properly, a worksheet must be active in Excel. It will generate an error if all windows are hidden, or if the active sheet is a Chart sheet

楼主可能是摘于美国John Walkenbach著的宝典丛书《EXCEL2003高级VBA编程宝典》的11.4.7《检索已经关闭的工作簿中的值》一节吧,在第256~257页中。

评分

1

查看全部评分

头像被屏蔽

TA的精华主题

TA的得分主题

发表于 2007-3-23 20:30 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-3-23 21:05 | 显示全部楼层

试了试确实不错,ExecuteExcel4Macro的实现机理是什么? XLM macro是什么呢,不明白

TA的精华主题

TA的得分主题

发表于 2007-3-23 21:09 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

ado也可以不打开读数据,但其实使用数据库方式打开,这种模式??

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-3-24 03:46 | 显示全部楼层

其实应用很方便,新建一个EXCEl文件,首先把自定义函数Private Function GetValue(path, file, sheet, ref) ...End Function 插入到你的模块中,然后把2个例子的代码再插入到你模块中,把里面的路径p=...、文件名f=...(工作簿)、工作表s=...、单元格a=...(第一个实例)替换成你的你要测试的文件,创建2命令按钮,然后就可以测试他们,操作正确的话,应该没问题。祝你成功! 注意:这个自定函数不能当函数用,只能用模块调用。

[此贴子已经被作者于2007-3-24 3:50:20编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-3-24 03:59 | 显示全部楼层
回5楼,我是从网搜到的,是John Walkenbach的免费下载代码,记得论坛有人求助过这样的代码,,感觉有价值,就贡献出来。感谢你的翻译!希望对大家有用!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-12-4 01:16 , Processed in 0.046871 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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