Excel VBA - Import range of data from Excel to Access table

Minnesota Mike

New Member
Joined
Apr 1, 2015
Messages
5
I am creating a macro in Excel 2010 that selects a range of data (which changes each month) and copies that range into a table in Access 2010. I am getting the error message "Run-time Error 2498: An expression you entered is the wrong data type for one of the arguments." The macro stops at the DoCmd.TransferSpreadsheet line. I think I have everything defined appropriately but something is a miss. The macro code is shown below. Thanks for input.

Code:
Sub Access_Export_Table()

    Dim app_Access As Access.Application
    Dim wb As Workbook
    Dim wb_Path As String
    Dim wb_Name As String
    Dim ws As Worksheet
    Dim int_Count_Rows As Integer
    Dim int_Count_Columns As Integer
    Dim rng_Export As Range
    
    Set wb = ActiveWorkbook
    wb_Path = wb.Path
    wb_Name = wb.Name
    
    Worksheets("Export").Activate
    Set ws = ActiveSheet
    Range("A1").Select
    int_Count_Rows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    int_Count_Columns = Selection.End(xlToRight).Column
    Set rng_Export = Range(Cells(1, 1), Cells(int_Count_Rows, int_Count_Columns))
    rng_Export.Select
    
    Set app_Access = Access.Application
 
    Application.ActivateMicrosoftApp xlMicrosoftAccess
    
    app_Access.DoCmd.TransferSpreadsheet _
        acImport, _
        acSpreadsheetTypeExcel12Xml, _
        "Imported Forecast", _
        wb_Path & "\" & wb_Name, _
        True, _
        rng_Export
        
End Sub
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The Range argument is expected to be in the form of a string. Try this...

Code:
    app_Access.DoCmd.TransferSpreadsheet _
        acImport, _
        acSpreadsheetTypeExcel12Xml, _
        "Imported Forecast", _
        wb_Path & "\" & wb_Name, _
        True, _
        rng_Export[COLOR=#FF0000].Address[/COLOR]


DoCmd.TransferSpreadsheet Method

Sorry for your loss (Go Packers).
 
Last edited:
Upvote 0
Thanks for the reply AlphaFrog. I applied your suggestion but that yielded another error.

The error now is "The Microsoft Access database engine could not find the object '$A$1:$N$4369'. Make sure the object exists and that you spell its name and the path name correctly. If '$A$1:$N$4369' is not a local object, check your network connection or contact the server administrator." The range referenced in the error message is the address of the range defined in macro as "rng_Export". I see in the Locals Window that the macro understands that is the address of the range.

The macro has already defined the path name and file name for the Excel workbook; do I also need to define the sheet name somewhere in the code? From the examples I have seen, it does not appear that is a requirement.

Thanks again for your assistance.

P.S.
And thanks for the condolences but I have mixed feelings. My heart was for the Vikings but my money was on the Packers.
 
Upvote 0
The macro has already defined the path name and file name for the Excel workbook; do I also need to define the sheet name somewhere in the code? From the examples I have seen, it does not appear that is a requirement.

I don't know if the sheet name is required. This reference suggest you do need it.
General: Importing Excel spreadsheets from code

Give it a shot.

Code:
    app_Access.DoCmd.TransferSpreadsheet _
        acImport, _
        acSpreadsheetTypeExcel12Xml, _
        "Imported Forecast", _
        wb.FullName, _
        True, _
        "Export!" & rng_Export.Address
 
Upvote 0
Finally got it solved!!!

The sheet name is required. However, the trick is that the address of the range can not be an absolute address; that is with "$" signs in the address.

I added the following statements:
Dim ws_Name As String
Dim str_Export_Address As String
Dim str_Export_SheetAddress As String
ws_Name = ws.Name
str_Export_Address = rng_Export.Address
str_Export_Address = WorksheetFunction.Substitute(str_Export_Address, "$", "")
str_Export_SheetAddress = ws_Name & "!" & str_Export_Address

I then changed the DoCmd.TransferSpreadsheet statement to be:
app_Access.DoCmd.TransferSpreadsheet _
acImport, _
acSpreadsheetTypeExcel12Xml, _
"Imported Forecast", _
wb_Path & "\" & wb_Name, _
True, _
str_Export_SheetAddress
 
Upvote 0
Glad you figured it out.

The range.Address property has arguments to exclude the $. In the vba Editor, highlight the Address keyword and press F1 for syntax help.

Also, you could use the workbook.FullName property to get both the path and filename of the workbook.

Code:
    app_Access.DoCmd.TransferSpreadsheet _
        acImport, _
        acSpreadsheetTypeExcel12Xml, _
        "Imported Forecast", _
        [COLOR="#FF0000"]wb.FullName[/COLOR], _
        True, _
        [COLOR="#FF0000"]"Export!" & rng_Export.Address(0, 0)[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,147
Members
449,364
Latest member
AlienSx

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