从工作簿的单元格中提取值而不打开它

问题描述 投票:0回答:1

我有一个名为

Excel_File.xlsx
的 Excel 文件,它保存在我当前的 Excel 工作簿的同一位置,并且该文件包含一个行数为 100、列数为 1000 的矩阵。

我想从第 4 列和第 5 行提取单元格的值而不打开它。文件

Excel_File.xlsx
的大小相当大,所以我忍住不打开它。

我使用下面的VBA代码没有成功

MsgBox ExecuteExcel4Macro("'" & ThisWorkbook.Path & "[" & "\Random_Number_Matrix.xls" & "]" & strSheet & "'!" & Range("A1").Address(1, 1, xlR1C1))

您能帮忙提供正确的 VBA 代码吗?

我还想传递不带单元格名称的行和列索引,即我想使用索引号

D5
,而不是传递
(5,4)

任何指针都会非常有帮助。

感谢您的宝贵时间。

excel vba
1个回答
1
投票

可以使用 ExecuteExcel4Macro 尝试从关闭的工作簿中读取

单个单元格值
。请尝试使用下一段代码:

Sub ExgtractONECellValFromClosedWB()
  Dim strPath As String, strFile As String, sheetName As String, ws As Worksheet
  Dim rng As Range: Set rng = cells(5, 4)
  
  Set ws = ActiveSheet 'sheet where to return
  
  strPath = ThisWorkbook.path & "\"
  strFile = "Excel_File.xlsx"
  sheetName = "Sheet1"

  'extract the necessary value in "A2" of the active sheet:
  ws.Range("A2").value = Application.ExecuteExcel4Macro("'" & strPath & _
      "[" & strFile & "]" & sheetName & "'!R" & rng.row & "C" & rng.column)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.