VBA - Retrieve Data from a Closed Workbook

andrea -  
 andrea -
Hello everyone,

I'm new to VBA and I have the following problem:

I have a file A and a source file. I want to extract data from the source file to display in file A. So I set up a link between the two files. However, this required opening the source file for file A to update.

I wrote the following code, which I managed to get with the help of a user:

Sub ImportDataWithoutOpening()

'Declare variable path and name as string
Dim SourcePath As String
Dim SourceFile As String

'Define variable content
SourcePath = "I:\G_AMEC\ETAT AMEC2\Etat AMEC 2\" '
SourceFile = "Etat AMEC 2 (version 2).xlsm"

'Create a data retrieval range from the closed source file, from the calculations sheet and the desired cell range
ThisWorkbook.Names.Add "range", _
RefersTo:="='" & SourcePath & "[" & SourceFile & "]calculs'!H9"

With Worksheets("RECAP") ' sheet from file A

Worksheets("RECAP").Range("F49").Value = "=range"
End With

End Sub

But this code displays 0 instead of the desired value... Do you have any ideas?

Thanks in advance

2 réponses

andrea
 
Patrice33740, great, it works! Thank you very much!

However, when I run the program, it keeps asking me to update the values: a dialog box opens and I have to select the source file. Do you know how to bypass this dialog box?

Thanks again.
5
Patrice33740 Posted messages 8400 Registration date   Status Membre Last intervention   1 781
 
Hi,

If the dialog box appears, it means that either the SourcePath or the SourceFile is incorrect, or there is no "calculs" sheet in the source file.
0
andrea
 
ok, thanks for your reply, I'll try to see what’s wrong!
0