Import data from Excel

joycesolomon

New Member
Joined
Aug 2, 2011
Messages
48
Hi,

I have an excel book, that i want to import the data from sheet TASK, column A and D into a table in access....each time this data is generated the amount of records varies.... the data has first two rows with two types of heading too


I created the below code in Access VBA

DoCmd.TransferSpreadsheet transfertype:=acImport, SpreadsheetType:=5, _
tablename:="TASK2", FileName:="C:\Documents and Settings\Desktop\Test.xls", _
Hasfieldnames:=True, Range:="Task!"


But i keep getting error that the field F1 is not present in the access table, can someone please help.....
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,

I am new at this, but I got that error yesterday. I believe I fixed it by making sure that my Access table had the same field names as the Excel spreadsheet.

Nancy
 
Upvote 0
I would probably try to name the range in Excel that you want to import (exclude one or both header rows, and only include column A-D), and use the named range in the Range argument of your TransferSpreadsheet command.

It is pretty easy to create an Excel macro to dynamically select the appropriate range each time, something like:
Code:
Sub MyNameRangeMacro()
 
    Dim myLastRow As Long
    Dim myRange As Range
 
'   Find last row with data
    myLastRow = Range("A2").SpecialCells(xlLastCell).Row
    
'   Set range to name
    Set myRange = Range("Sheet1!A2:D" & myLastRow)
 
'   Name range ("ImportRange")
    ActiveWorkbook.Names.Add Name:="ImportRange", RefersTo:=myRange
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top