Solved

Import thousands of worksheet columns into Access table

Posted on 2011-03-16
5
454 Views
Last Modified: 2012-05-11
I have a Excell Spreadsheet that has over 5,000 worksheets. Each worksheet has the same exact number of column and column names; and each worksheet has at least one row of data. I'm currently using the following VB code to import but it is extremely, extremely slow:
 
Const acImport = 0
Const acSpreadsheetTypeExcel9 = 8

Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase "C:\SomeFolder\data.accdb"

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

strFileName = "C:\SomeFile\File.xlsx"

Set objWorkbook = objExcel.Workbooks.Open(strFileName)
Set colWorksheets = objWorkbook.Worksheets

For Each objWorksheet in colWorksheets 
    Set objRange = objWorksheet.UsedRange 
    strWorksheetName = objWorksheet.Name & "!" & objRange.Address(False, False) 
    objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        "Vulnerability", strFileName, True, strWorksheetName
Next

Open in new window

Does anyone know a faster or more effecient way to get this done?

(Excel and Access 2007)
0
Comment
Question by:thomasd04
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 35150083
what about putting all rows into one worksheet with worksheetname in another column
then load all data to db in one shot!
0
 
LVL 3

Author Comment

by:thomasd04
ID: 35150795
Hi HainKurt. I'm not quite sure what you are suggestion. Could you clarify a bit more? Right now the spreadsheet is being created by an external application and we are unable to modify how it outputs to Excel. Now that it's been outputted in this format, I need to find a way to get it into a database.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35151168
what I say is, instead of loading each worksheet into db one by one,
create a new excel file with one worksheet
then with vba code, collect all data from worksheets and put into the new excel file (add a new column to store worksheetname)
then load this worksheet into db in one shot...
0
 
LVL 3

Author Comment

by:thomasd04
ID: 35151194
ahhhh...okay. That makes sense. Could you help me with the code? I'm not good with VB, it took me forever to write the little code I posted above.
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35151205
like this


strFileName = "C:\SomeFile\File.xlsx"
strTargetFileName = "C:\SomeFile\output.xlsx"

Set objWorkbook = objExcel.Workbooks.Open(strFileName)
Set objWorkbookNew = objExcel.Workbooks.Open(strTargetFileName)
Set colWorksheets = objWorkbook.Worksheets

For Each objWorksheet in colWorksheets 
    Set objRange = objWorksheet.UsedRange 
    strWorksheetName = objWorksheet.Name & "!" & objRange.Address(False, False) 
     ... copy data into objWorkbookNew, with one worksheet with name say data, with e3xtra column = strWorksheetName ...
Next 

objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        "Vulnerability", strTargetFileName, True, "data"

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now