Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Import thousands of worksheet columns into Access table

Posted on 2011-03-16
5
Medium Priority
?
484 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 61

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 61

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 61

Accepted Solution

by:
HainKurt earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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