Results 1 to 3 of 3

import all sheets from a closed workbook

Threaded View

  1. #1
    Registered User
    Join Date
    06-01-2005
    Posts
    3

    import all sheets from a closed workbook

    Hi,

    I need to import all sheets from a some closed workbooks to my active 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 As 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!
    Last edited by johnpetrusa; 06-01-2005 at 04:03 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1