473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Import Multiple Excel Sheets into Access

I have a spreadsheet with about 35 tabs that I need to import into one table in Access. I found this link http://www.thescripts.com/forum/thre...iple+tabs.html and changed some things within the code.

First off - I've written a piece of code that changes the name of each sheet (Sheet1, Sheet2, etc), deletes the blank rows and formats a couple of the columns to text and date format. It then saves and quits the application as well as sets it to nothing.

When I try to run the code that is within the above link I get
*RunTime Error '91' Object variable or With block variable not set*

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. Dim excelapp As New Excel.Application
  2. Dim excelbook As New Excel.Workbook
  3. Dim excelsheet As New Excel.Worksheet
  4. Dim intNoOfSheets As Integer, intCounter As Integer
  5. Dim strFilePath As String, strLastDataColumn As String
  6. Dim strLastDataRow As String, strLastDataCell As String
  7.  
  8. strFilePath = "\\drivemapping\spreadsheet.xls"
  9.  
  10. Set excelbook = excelapp.Workbooks.Open(strFilePath)
  11.  
  12. intNoOfSheets = excelbook.Worksheets.Count  <--It finds 35 sheets
  13.  
  14. For intCounter = 3 To intNoOfSheets 'start at sheet 3
  15.   excelbook.Worksheets(intCounter).Activate
  16.     'The next 3 lines will obtain the last data cell reference for each Worksheet
  17.     strLastDataColumn = Chr(Selection.SpecialCells(xlLastCell).Column + 64) <--RUNTIME ERROR '91' HERE
  18.     strLastDataRow = Selection.SpecialCells(xlLastCell).Row
  19.         If strLastDataRow = 1 Then   'sheet has no rows to import
  20.         GoTo Line1
  21.         End If
  22.     strLastDataCell = strLastDataColumn & strLastDataRow    
  23.       DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel11, "tmp", strFilePath, False, _
  24.                  excelbook.Worksheets(intCounter).Name & "!A2:" & strLastDataCell  'A1 is header row so start on A2
  25. Line1:
  26. Next
  27.  
  28. excelbook.Close
  29. excelapp.Quit
  30. Set excelapp = Nothing
However...it doesn't do it ALL the time. I just got the error...ended it and then re-ran and it worked fine. I'm using Access 2003 on WinXP SP2. Just want it to be as stable as it possibly can be before I turn it over to the user.

I looked up the error message here & on google but none of the explanations really applied to my scenario. Any help is much appreciated.
Oct 29 '07 #1
1 10794
FishVal
2,653 Expert 2GB
Hi, there.

Try to refer Selection object through Excel.Application object.
e.g.
instead
Selection.blablabla
excelapp.Selection.blablabla
Oct 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: sunilkeswani | last post by:
I need help with exporting data from 2 access tables, into 2 existing spreadsheets in a single Excel file. Currently, I am using this code: DoCmd.TransferSpreadsheet acExport, 8, "Table1",...
8
by: Nick M | last post by:
Hello All, Excellent info here Thanks! I am very new to using access in general and I am on a learning curve. I'm trying to import an excel workbook (with worksheets) into an access db via a...
10
by: Niklas | last post by:
Hi Before I start coding I need to be sure that nobody else has not done it yet and that I can use it. I need an import utility which import data from Excel to a database or some object in...
2
by: madeleine | last post by:
I'm hoping the answer to this is that I'm just doing something silly, but I'm really scratching my head over this one. I'm importing data from multiple workbooks, each workbook has a sheet called...
8
by: FireGeek | last post by:
I have a database that is split appropriately. Annually, we need to add data from .xls files. I have added code so with a click of a button, it will import this data as a new table into the...
6
nehashri
by: nehashri | last post by:
hello i have my data stored in different Ms Excell sheets. now I am planning to make a database of the data i have. but i m confused whether to use Postgresql or mySQL for the same. i had plans to...
3
by: Roy | last post by:
Hi All, I have an A2003 application .Recently we had a request to import data from IDL files and create some reports. Since I have no idea to do that using Access,I used Excel to import data and...
4
by: Harshe | last post by:
hello all, I am trying to code, but i am just stuck after importing one sheet. so here is the gist of what i need help with. In a workbook at the start of the year (january) i will have 4...
7
by: ivancycheng | last post by:
I have around 400 excel files with same format (multiple sheets) and wants to import few sheets from excel to MS Access (XP version) for further processing. any one can advise me how to do it? (I'm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.