473,382 Members | 1,369 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,382 software developers and data experts.

Import Excel to Access

Hi,

I'm trying to import an excel file into a access table. The problem im having is that rows that have no data are being added to the table. As the amount of rows will change depending on the file, I dont want to just set a range of x amount of rows. Is there a way around this problem?

this is the code I use at the moment.

ImportName = Text1.Value


DoCmd.TransferSpreadsheet transfertype:=acImport, tablename:="tblLiveData", FileName:=ImportName, HasFieldNames:=True, Range:="Sheet1!A:P"


Thanks for your help
Mar 20 '08 #1
7 1972
Stewart Ross
2,545 Expert Mod 2GB
Hi. If there are blank rows in your Excel table the import is just faithfully replicating the blank rows. Why not import them just as they are, and once imported base all of your further processing on an Access query. The query could exclude rows automatically if you define the criteria - for example, where there is a null value in a particular field.

I recommend the 'base it on a query' approach for three reasons. Firstly, it is good practice to base forms, reports etc on queries rather than directly on the underlying table (as you have total control of the sort order and so on by doing so, and can produce views of the data that include calculated fields, say, or joined information which is not available when working directly on the table itself).

Secondly, you would have to code an import routine yourself in VBA as you would no longer be able to use the TransferSpreadsheet method you are currently using.

Lastly, the imported data would be a faithful copy of what was in Excel. I think this is also important, because if you have to compare before and after views of the data transferred for any reason you don't want to build in differences that make comparisons difficult.

You could also look at why your Excel table has blank rows in it and address that instead.

-Stewart

ps I'm assuming that you need to specify the range, as in your code you have explicitly defined rows A to P of sheet1 for import. If some of these are blank at the end, why do you need to set a row range at all? It is an optional parameter and if left off Access will simply import the whole sheet from the first to the last row.
Mar 20 '08 #2
Thanks for that,

I need the data from columns A to P, but as the number of rows in the spreadsheet will vary, I need the range to expand/decrease to include all the rows, but not the rows that dont have any data in i.e. if only 3 rows of data is input, the range would be A1:P4. I know you can create a dynamic range in excel using OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1)) but is there a way of using this range in access?

Any help is appreciated.
Mar 20 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
I need the data from columns A to P, but as the number of rows in the spreadsheet will vary, I need the range to expand/decrease to include all the rows, but not the rows that dont have any data in i.e. if only 3 rows of data is input, the range would be A1:P4. I know you can create a dynamic range in excel using OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1)) but is there a way of using this range in access?
Hi. It is possible to write a function in VBA code to open an instance of Excel, open your spreadsheet, retrieve the last cell reference, and close the file again. This could potentially be used to feed the range in your TransferSpreadsheet call. I haven't done it myself, so at this stage I have not got a function which would do so ready 'off the shelf' - perhaps other respondents might.

I would still suggest it is far simpler to use a query to exclude blank rows, however. I have to ask: how many rows of blank data are there in your sheet, and in what way would these pose any sort of a problem to Access?

-Stewart
Mar 20 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi again. As a further thought, you could import the whole sheet by dropping the A:P range requirement altogether. This would mean you are not importing blank rows, but you would be importing all columns. You would then only include in your query on the base table the fields from columns A to P. Again, this is not a problem for Access.

-Stewart
Mar 20 '08 #5
Thanks for your help Stewart,

I decided on a much simpler solution, using temporary tables, append and delete queries to move the data to my main table. A dynamic range in access would have been nice, but I guess going round the houses is better than not going at all.. :)

Many Thanks for your help in this matter.

Mark
Mar 20 '08 #6
Stewart Ross
2,545 Expert Mod 2GB
I decided on a much simpler solution, using temporary tables, append and delete queries to move the data to my main table. A dynamic range in access would have been nice, but I guess going round the houses is better than not going at all.. :)
Hi Mark. It was too intriguing a challenge to let go, so if at sometime you still want to pursue a dynamic solution here is a function which will return the last row reference of a given Excel file as a Long integer value. You can use this with your restricted column P range value as shown below.

To use it, insert the function into an existing code module in Access, or create a new module if you don't have one already. From the VB editor add a reference to the Microsoft Excel object library (Tools, References, tick Microsoft xx.x Object Library if it is not already ticked).

Back on the form in which you are doing the transfer add the following code:
Expand|Select|Wrap|Line Numbers
  1. Dim LastRow as Long
  2. Dim strRange as String
  3. LastRow = WorkSheetLastRow(ImportName) ' this must come after the ImportName assignment statement you showed in your code
  4. strRange = "Sheet1!A1:P" & LastRow
  5. DoCmd.TransferSpreadsheet <...others as before>,  Range := strRange
Expand|Select|Wrap|Line Numbers
  1. Public Function WorkSheetLastRow(Filename As String, Optional Sheetname As String = "") As Long
  2. 'IN:  Filename (including path) of Excel file to open
  3. 'IN:  Sheetname (optional) to open within the workbook. If not specified
  4. '     the default is the first sheet normally opened.
  5. 'OUT: the row number of the last cell in the spreadsheet.
  6.     On Error GoTo CantOpen
  7.     Dim objExcel As Excel.Application
  8.     Dim WS As Excel.Worksheet
  9.     Dim WsRange As Excel.Range
  10.     Dim LastRow As Long
  11.     Dim ErrMsg As String
  12.     Set objExcel = New Excel.Application
  13.     objExcel.Workbooks.Open (Filename)
  14.     If Sheetname = "" Then
  15.         Set WS = objExcel.ActiveSheet
  16.     Else
  17.         Set WS = objExcel.Worksheets(Sheetname)
  18.     End If
  19.     Set WsRange = WS.Range("A1").SpecialCells(xlCellTypeLastCell)
  20.     LastRow = WsRange.Row
  21.     objExcel.ActiveWorkbook.Close
  22.     Set objExcel = Nothing
  23.     WorkSheetLastRow = LastRow
  24.     Exit Function
  25. CantOpen:
  26.     ErrMsg = "Cannot open File " & Filename
  27.     If Sheetname <> "" Then
  28.         ErrMsg = ErrMsg & ", Worksheet " & Sheetname
  29.     End If
  30.     Call MsgBox(ErrMsg, vbCritical, "Excel file open error")
  31.     Set objExcel = Nothing
  32. End Function
-Stewart

For interest I also wrote a more general function to return the range in "A1:ZZnnn" format which I also include here.

Expand|Select|Wrap|Line Numbers
  1. Public Function WorkSheetRange(Filename As String, Optional Sheetname As String = "") As String
  2. 'IN:  Filename (including path) of Excel file to open
  3. 'IN:  Sheetname (optional) to open within the workbook. If not specified
  4. '     the default is the first sheet normally opened.
  5. 'OUT: the range reference to the last cell, in format "A1:XXNN" where
  6. '     XXNN is the last column and row in A1 to ZZ65535 format
  7.     On Error GoTo CantOpen
  8.     Dim objExcel As Excel.Application
  9.     Dim WS As Excel.Worksheet
  10.     Dim WsRange As Excel.Range
  11.     Dim LastRow As Long
  12.     Dim LastCol As Long
  13.     Dim Result As String
  14.     Dim ErrMsg As String
  15.     Set objExcel = New Excel.Application
  16.     objExcel.Workbooks.Open (Filename)
  17.     If Sheetname = "" Then
  18.         Set WS = objExcel.ActiveSheet
  19.     Else
  20.         Set WS = objExcel.Worksheets(Sheetname)
  21.     End If
  22.     Set WsRange = WS.Range("A1").SpecialCells(xlCellTypeLastCell)
  23.     LastRow = WsRange.Row
  24.     LastCol = WsRange.Column
  25.     objExcel.ActiveWorkbook.Close
  26.     Set objExcel = Nothing
  27.     Result = "A1:" & ColumnRef(LastCol) & CStr(LastRow)
  28.     WorkSheetRange = Result
  29.     Exit Function
  30. CantOpen:
  31.     ErrMsg = "Cannot open File " & Filename
  32.     If Sheetname <> "" Then
  33.         ErrMsg = ErrMsg & ", Worksheet " & Sheetname
  34.     End If
  35.     Call MsgBox(ErrMsg, vbCritical, "Excel file open error")
  36.     Set objExcel = Nothing
  37. End Function
  38. Public Function ColumnRef(NumericID As Long) As String
  39. 'IN: valid column number for Excel worksheet
  40. 'OUT: string representing column reference in "A" to "ZZ" format
  41.     Dim Remainder As Long
  42.     Dim HowMany As Long
  43.     Dim Result As String
  44.     Const ChangesAt = 26
  45.     HowMany = Int((NumericID - 1) / ChangesAt)
  46.     Remainder = NumericID - ChangesAt * HowMany
  47.     Result = Chr(Asc("A") + Remainder - 1)
  48.     If HowMany > 0 Then
  49.         Result = Chr(Asc("A") + HowMany - 1) & Result
  50.     End If
  51.     ColumnRef = Result
  52. End Function
Mar 20 '08 #7
Hi Stewart,

Thanks for that, it must have taken you ages! As I said before, I decided on a simpler solution, but will deffinately look at what you have posted as it might be a better way of doing this.

Many Thanks for your help.


Mark
Mar 25 '08 #8

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

Similar topics

1
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
2
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
1
by: John Hewitt | last post by:
A 'newbie' question - importing into Access from Excel. Googled the group postings but I can't find the amswer I have a 50 line three column file in Excel that contains Names and Tel numbers....
3
by: deko | last post by:
I've been trying to use the Access Import Wizard to expedite importing data into my mdb. The nice thing about the wizard is that I can import from different file formats - txt, xls, even Outlook -...
15
by: daniroy | last post by:
Hello everybody, your help will be very very welcome on a very classic question... I have some Excell Macro background, a little bit of VBA knowledge. I import daily from Internet series of...
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...
2
by: john | last post by:
I have 400 different Excel-spreadsheetfiles, same structure, all with only one record in it, and all residing in the same folder. Every now and then new Excel files are being added. In my Access...
11
by: MD | last post by:
Hello, I need to import a sheet of 884 different excel-file with same lay- out. The sheet name is 'Totaal' and is the same in all different files. Is there a script (module) in order to: 1....
3
by: MarcGA | last post by:
(Excel 2003, Access 2003, XP, novice user here) I can't get Access to accept multiple Excel files to the same table. I can import the spreadsheets to a new table, but I need to import 23...
8
by: shenkel55 | last post by:
I'm using Access and Excel 2003. Using either the import wizard or code, I have the same problem. This problem only happens with Excel files automatically generated by Corp IT. If I try to do an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.