Import Excel Sheet

J

Jim

How do I reference the Sheets(Index) in a VBA
Docmd.Transferspreadsheet command? Thanks.
 
K

Ken Snell

You don't do it directly.

What you need to do is to use a variable that gets the actual name of the
each sheet in the index and then use that variable to concatenate with the
"!" ending delimiter (and perhaps to surround the text string with ' marks
if it contains spaces), and then use that concatenated variable as the
argument for the sheet range in the TransferSpreadsheet command.

Something like this:

For lngSheet = 1 To Excel.Workbooks("NameOfFile").Worksheets.Count
strSheet = Excel.Workbooks("NameOfFile").Worksheets(lngSheet).Name
If InStr(strSheet, " ") > 0 Then strSheet = "'" & strSheet & "'"
strSheet = strSheet & "!"
DoCmd.TransferSpreadsheet Range:=strSheet, .....
Next lngSheet

(.... should be replaced by the other arguments).
 
J

jim

Thanks, worked great.
-----Original Message-----
You don't do it directly.

What you need to do is to use a variable that gets the actual name of the
each sheet in the index and then use that variable to concatenate with the
"!" ending delimiter (and perhaps to surround the text string with ' marks
if it contains spaces), and then use that concatenated variable as the
argument for the sheet range in the TransferSpreadsheet command.

Something like this:

For lngSheet = 1 To Excel.Workbooks ("NameOfFile").Worksheets.Count
strSheet = Excel.Workbooks("NameOfFile").Worksheets (lngSheet).Name
If InStr(strSheet, " ") > 0 Then strSheet = "'" & strSheet & "'"
strSheet = strSheet & "!"
DoCmd.TransferSpreadsheet Range:=strSheet, .....
Next lngSheet

(.... should be replaced by the other arguments).


--
Ken Snell
<MS ACCESS MVP>




.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top