Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default import all sheets from a closed workbook


Hi,

I need to import all sheets from a some closed workbooks to my activ
workbook

some like:

row1: workbookname - worksheet_name1
row2-xxx: --all content of worksheet(1)--

rowXX: workbookname - worksheet_name2
rowXX: --all content of worksheet(2)--

All in the same worksheet, one below the other.

I´m using a sub:

Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange A
String, _
TargetRange As Range, IncludeFieldNames As Boolean)
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
Dim TargetCell As Range, i As Integer
dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};"
"ReadOnly=1;DBQ=" & SourceFile
Set dbConnection = New ADODB.Connection
On Error GoTo InvalidInput
dbConnection.Open dbConnectionString
Set rs = dbConnection.Execute("[" & SourceRange & "]")
Set TargetCell = TargetRange.Cells(2, 1)
If IncludeFieldNames Then
For i = 0 To rs.Fields.Count - 1
TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
Next i
Set TargetCell = TargetCell.Offset(1, 0)
End If
TargetCell.CopyFromRecordset rs
rs.Close
dbConnection.Close
Set TargetCell = Nothing
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Sub

InvalidInput:
MsgBox "Error opening file." & vbCrLf & SourceFile & " not found!"
vbExclamation, "Import"

End Sub

But .. this only form for workSheet(1)

some idea?

Tnx

--
johnpetrus
-----------------------------------------------------------------------
johnpetrusa's Profile: http://www.excelforum.com/member.php...fo&userid=2395
View this thread: http://www.excelforum.com/showthread.php?threadid=37577

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default import all sheets from a closed workbook


Hello

you may try


Sub importDatas_FromAllSheets_ClosedWorkbook()
'Activate Microsoft ActiveX Data Objects x.x Library
'Activate Microsoft ADO Ext 2.7 for DLL ans Security
Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cat As ADOX.Catalog
Dim xConnect As String, Fichier As String, Cible As String
Dim Feuille As ADOX.Table
Dim i As Integer
Dim j As Byte

Fichier = "C:\closedWorkbook.xls"
i = 1
xConnect = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
"ReadOnly=1;DBQ=" & Fichier

Set Cat = CreateObject("ADOX.Catalog")
Set Cn = CreateObject("ADODB.Connection")

Cn.Open xConnect
Set Cat.ActiveConnection = Cn

For Each Feuille In Cat.tables
Cells(i, 1) = Fichier
Cells(i, 2) = Feuille.Name

i = i + 1
For j = 1 To Feuille.Columns.Count
Cells(i, j) = Feuille.Columns(j - 1).Name
Next j

Cible = "SELECT * FROM [" & Feuille.Name & "];"

Set Rs = New ADODB.Recordset
Rs.Open Cible, xConnect, adOpenStatic, adLockOptimistic, adCmdText
i = i + 1
Cells(i, 1).CopyFromRecordset Rs
i = i + Rs.RecordCount + 1
Next

Cn.Close
Rs.Close
Set Cn = Nothing
Set Rs = Nothing

End Sub



Regards ,
miche

--
michelxl
-----------------------------------------------------------------------
michelxld's Profile: http://www.excelforum.com/member.php...fo&userid=1736
View this thread: http://www.excelforum.com/showthread.php?threadid=37577

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default import all sheets from a closed workbook


Thanks!, it work perfect!

--
johnpetrus
-----------------------------------------------------------------------
johnpetrusa's Profile: http://www.excelforum.com/member.php...fo&userid=2395
View this thread: http://www.excelforum.com/showthread.php?threadid=37577

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import data from a closed workbook David T Excel Discussion (Misc queries) 2 September 17th 07 07:24 PM
Import Sheet from Closed Workbook Joe Derr[_2_] Excel Programming 1 May 2nd 05 09:07 AM
Possible to copy sheets into another (closed!) workbook? Grotifant Excel Discussion (Misc queries) 5 February 23rd 05 08:55 AM
Using Querytable.add to import data from a closed workbook Markus Stolle[_5_] Excel Programming 8 August 3rd 04 08:51 PM
Accessing All Sheets in a closed workbook jwallace Excel Programming 4 May 20th 04 10:33 AM


All times are GMT +1. The time now is 03:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"