+ Reply to Thread
Results 1 to 3 of 3

Import data from multiple .mdb to multiple sheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-21-2013
    Location
    KL
    MS-Off Ver
    Excel 2010
    Posts
    168

    Import data from multiple .mdb to multiple sheets

    Hi all,

    I want to update data from multiple .mdb files to multiple existing sheets at the same cell location, in one workbook.
    All the .mdb files have the exact same format and I named them the same, "Raw".
    My intended result is to update/match the data with the same file name and sheet name, i.e. A.mdb to Sheet A, B.mdb to Sheet B and etc.
    I have a named range Range("FileName") where I listed down all the source files, so that the macro will loop for each name in the list.

    Below is the current code that I have:

    Sub import_access()
        Dim sSQL    As String               
        Dim cnt     As ADODB.Connection    
        Dim rst     As ADODB.Recordset      
        Dim rngCell As Range
        Dim rngFileName As Range
        
        On Error Resume Next
        
        Set rngFileName = Range("FileName")           '/ File Name of source file .mdb
        
        For Each rngCell In rngFileName
        
            If rngCell <> "" Then
        
                sSQL = "SELECT START_TIME,ONCO,OAHT FROM [Raw]"
                Set cnt = New ADODB.Connection
                Set rst = New ADODB.Recordset
        
                With cnt
                    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; " & _
                    "Data Source=" & "C:\Users\hyeap\Desktop\" & rngCell & ".mdb" & ";" & "Jet OLEDB"
                    .Open
                End With
        
                rst.Open sSQL, cnt, adOpenDynamic, adLockPessimistic
                Sheets(rngCell).Range("A40").CopyFromRecordset rst
    
                'rst.Update
                rst.Close
                Set rst = Nothing
                cnt.Close
                Set cnt = Nothing
        
            End If
        
        Next rngCell
    End Sub
    I got an error and I think it's because the destination sheet name cannot be recognised (dim as range).
    Appreciate if anyone can assist.

    Thank you.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,313

    Re: Import data from multiple .mdb to multiple sheets

    Try:

            Sheets(rngCell.Value).Range("A40").CopyFromRecordset rst

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    11-21-2013
    Location
    KL
    MS-Off Ver
    Excel 2010
    Posts
    168

    Re: Import data from multiple .mdb to multiple sheets

    Hi TMS,

    Thank you for your reply.

    Never thought that a small trick will do the magic!

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Import multiple sheets into open workbook
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-27-2014, 11:48 AM
  2. Replies: 0
    Last Post: 01-28-2013, 02:25 PM
  3. Collecting Data from multiple sheets of a workbook and import them into new workbook
    By tufanayd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2009, 04:10 PM
  4. Replies: 2
    Last Post: 08-30-2007, 12:34 PM
  5. [SOLVED] how do i use import to consolidate data from multiple sheets
    By Naveen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2006, 07:45 AM

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