hi Avi,
i put the range names in named_range () var
Dim named_range()
For Each sSheetName In oCat.Tables
If Not Right(sSheetName.Name, 1) = "$" Then
ReDim Preserve named_range(n)
named_range(n) = sSheetName.Name
n = n + 1
End If
Next
and i used that result in cn.Execute
For i = LBound(named_range) To UBound(named_range)
rw = Cells(65000, 1).End(xlUp).Row + 1
Set rs = cn.Execute("SELECT * FROM " & named_range(i))
Cells(rw + 1, 1) = named_range(i) ' modify destination cell
Cells(rw + 2, 1).CopyFromRecordset rs ' modify destination cell
Next
the full code:
'______________________________________________________________________
Sub copy_cells_of_named_range_from_closed_workbook()
'Need to activate the reference Microsoft ADO ext x.x for DLL and Security
'Need to activate the reference Microsoft ActiveX Data Objects x.x Library
Dim cn As ADODB.Connection
Dim oCat As ADOX.Catalog
Dim oFile As String, Resultat As String
Dim oSheet As ADOX.Table
Dim named_range()
oFile = "C:\MyFile.xlsm"
Set cn = New ADODB.Connection
Set oCat = New ADOX.Catalog
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& oFile & ";Extended Properties=""Excel 12.0;HDR=YES;"""
.Open
End With
Set oCat.ActiveConnection = cn
For Each sSheetName In oCat.Tables
If Not Right(sSheetName.Name, 1) = "$" Then
ReDim Preserve named_range(n)
named_range(n) = sSheetName.Name
n = n + 1
End If
Next
For i = LBound(named_range) To UBound(named_range)
rw = Cells(65000, 1).End(xlUp).Row + 1
Set rs = cn.Execute("SELECT * FROM " & named_range(i))
Cells(rw + 1, 1) = named_range(i) ' modify destination cell
Cells(rw + 2, 1).CopyFromRecordset rs ' modify destination cell
Next
Set sSheetName = Nothing
Set oCat = Nothing
cn.Close
Set cn = Nothing
End Sub
'____________________________________________________________________
isabelle
Le 2014-05-15 11:33, (e-mail address removed) a écrit :
On Tuesday, April 22, 2014 10:18:17 PM UTC+3, isabelle wrote:
Hello again,
I was able to connect and retrieve the names, but the problem seems that the name
has no use in closed workbook as I can't refer to it in any function or in other
words, I can't know its range address...