Import/append Excel workbook with multiple sheets using VBA.

IMNovice

New Member
Joined
Jun 23, 2005
Messages
27
Each worksheet has a named range.
How to write VBA code to import/append into 1 access table
using the named ranges. I know how to use the TransferSpreadsheet method for 1 worksheet
and use the range name, but not sure how tp write the loop to get the
multiple range names from all worksheets with range names.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I think you only need to run your transferspreadsheet X times (once for each range name).
 
Upvote 0
Yes, I know that. My question was how to use the named ranges in a worksheet as a variable. Then create a loop to perform the import process X times using the variable to get the correct named range from each worksheet. Thanks.
 
Upvote 0
Not sure if I follow you precisely.

I would put the range names in an array, possibly:
Code:
Dim arr(0 To 2) As String

arr(0) = "RangeName1"
arr(1) = "RangeName2"
arr(2) = "RangeName3"

For i = 0 To 2
    DoCmd.TransferSpreadsheet acImport, , "MyTableName", "myfile.xlsx", True, arr(0)
Next i

If I only had a few range names I might dispense with the array:
Code:
DoCmd.TransferSpreadsheet acImport, , "MyTableName", "myfile.xlsx", True, "RangeName1"
DoCmd.TransferSpreadsheet acImport, , "MyTableName", "myfile.xlsx", True, "RangeName2"
DoCmd.TransferSpreadsheet acImport, , "MyTableName", "myfile.xlsx", True, "RangeName3"
 
Upvote 0
Wouldn't that be
DoCmd.TransferSpreadsheet acImport, , "MyTableName", "myfile.xlsx", True, arr(i)

This looks more like an Excel question but it's posted in the Access forum. Thus I wonder if there is not a way to loop through ranges in Excel (my Excel vba is rusty). However, I find the information conflicting. First post indicates each of several sheets has one range
Each worksheet has a named range.
then there is
My question was how to use the named ranges in a worksheet as a variable.
I have dealt with named ranges when automating Excel from Access but IIRC, there was only one on a sheet.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,179
Members
448,871
Latest member
hengshankouniuniu

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