VBA从已关闭工作簿中的命名单元格返回单元格值

时间:2016-07-06 23:01:15

标签: vba excel-2010

我试图在不打开源文件的情况下从另一个工作簿中检索单元格值。当我使用实际的单元格地址时,下面将返回“SourceRange”中的值。但是,我想修改下面的代码来引用单元格名称而不是单元格地址,因为可能会添加行。因此,如果源工作簿中的单元名称被命名为“Total”,那么我有办法检索该值吗?

Sub OpenFiles()

Dim Folder_YR As String
Dim Folder_MO As String
Dim Path As String
Dim File As String
Dim SourceSheet As String
Dim SourceRange As String

Folder_YR = Sheets("Inputs").Range("B5").Value
Folder_MO = Sheets("Inputs").Range("B9").Value
FN = Sheets("Inputs").Range("B10").Value


Path = "G:\PMT ACTIVITY\" & Folder_YR & "\" & Folder_MO & "\"
File = FN

SourceSheet = "Summary"
SourceRange = Range("C1").Address(1, 1, xlR1C1)

Ref = "'" & Path & "[" & File & "]" & SourceSheet & "'!" & SourceRange

Sheets("Model").Range("C18").Formula = ExecuteExcel4Macro(Ref)

End Sub

1 个答案:

答案 0 :(得分:-1)

格式类似于'C:\Book1.xlsx'!Total

https://www.ablebits.com/office-addins-blog/2015/12/08/excel-reference-another-sheet-workbook/#reference-to-defined-name-another-workbook

[Model!C18].Formula = "='" & Path & File & "'!Total"