VBA - 无需打开即可从其他工作簿中获取单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14192610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:05:01  来源:igfitidea点击:

VBA - Get Cell Values from other workbook without opening

vbacellexcel

提问by Ran Gualberto

I'm working to get the cell value of other workbook without opening it.

我正在努力在不打开它的情况下获取其他工作簿的单元格值。

And here's some of my codes:

这是我的一些代码:

Range("F3")= "='C:\inetpub\vhosts\hotdogko.com\httpdocs\private\excel\[Launch Pad.xls]Sheet1'!$B$12 "

Range("F3")= "='C:\inetpub\vhosts\hotdogko.com\httpdocs\private\excel\[Launch Pad.xls]Sheet1'!$B$12 "

This code is working well when data type of the cell value to pull is Date, Integer or Any not String data type. But it wont work correctly to string data type, it just returning #N/A.

当要提取的单元格值的数据类型为日期、整数或任何非字符串数据类型时,此代码运行良好。但是它不能正确地处理字符串数据类型,它只是返回#N/A。

Thanks for someone who can give me an answer for this problem.

感谢有人可以给我这个问题的答案。

回答by Chan Yoong Hon

You can try with following answer

您可以尝试以下答案

Sub ReadDataFromAnotherWorkBook()

    ' Open Workbook A with specific location
    Dim src As Workbook
    Set src = Workbooks.Open("C:\Users\chan.yoonghon\Desktop\Excel\BookA.xlsx", True, True)

    Dim valueBookA As Integer
    Dim valueBookB As Integer

    valueBookA = src.Worksheets("sheet1").Cells(1, 1)
    Cells(1, 1).Value = valueBookA

    ' Close Workbooks A
    src.Close False
    Set src = Nothing

     ' Dialog Answer
    MsgBox valueBookA
End Sub

回答by hugo

If you add the text function after the equal It should work something like:

如果在 equal 之后添加 text 函数它应该像这样工作:

Range("F3")= "=text("'C:\inetpub\vhosts\hotdogko.com\httpdocs\private\excel\[Launch Pad.xls]Sheet1'!$B " ;"") "

Perhaps you should check if the "are correct because sometimes you have to add double "".

也许您应该检查"它们是否正确,因为有时您必须添加 double ""

回答by Tomasz Spencer

Try it without using quotation marks (" "). It should help.

尝试不使用引号 (" ")。它应该有帮助。