Worksheet name

E

Ed

I have the following code:

Set oWb = Workbooks.Open(fname)

oWb.Sheets("Test41").Range("A1:AQ100").Copy

What i need to do is substitute the sheet name("Test41")
with a value in list box I have created on the sheet. I
have tried this with a cell reference but i get the
messasge 'Subscipt out of range'

Any have any clever ideas??
 
T

Tom Ogilvy

Dim oWb as Workbook
Dim sh as Worksheet
Dim shName as String
Set oWb = Workbooks.Open(fname)

shName = oWb.Worksheets("Sheet1").Range("A1").Text
on Error Resume next
set sh = oWb.Worksheets(shName)
On error goto 0
if sh is nothing then
msgbox "No sheet named ->" & shName & "<-"
Else
sh.Range("A1:AQ100").Copy
End if

If the name is a valid sheet name, it should work.
 

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

Similar Threads

Importing data 3
Copy - Paste 5
Last row 1
Import Wizard 1
Copy range between workbooks 2
Subscript out of range 1
Closing workbook 4
Delete Sheets 1

Top