Setting Worksheet

HWL

Active Member
Joined
Dec 1, 2009
Messages
462
This should work...

Dim tempfile1 As Workbook
Dim wsSheet As Worksheet
Set tempfile1 = Workbooks.Open(Range("D2"), ReadOnly, notify = False)
Set wsSheet = tempfile1.Worksheets(range("n2"))
MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)

If fails on Set wsSheet = tempfile1.Worksheets(range("n2"))

I can use Set wsSheet = templfile1.Worksheets(1) and it will work fine, but I'm not certain the sheet will always be indexed as 1, thus the reason I want the user to supply the sheet name in cell N2.

What am I doing wrong here?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This should work...

Dim tempfile1 As Workbook
Dim wsSheet As Worksheet
Set tempfile1 = Workbooks.Open(Range("D2"), ReadOnly, notify = False)
Set wsSheet = tempfile1.Worksheets(range("n2"))
MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)

If fails on Set wsSheet = tempfile1.Worksheets(range("n2"))

I can use Set wsSheet = templfile1.Worksheets(1) and it will work fine, but I'm not certain the sheet will always be indexed as 1, thus the reason I want the user to supply the sheet name in cell N2.

What am I doing wrong here?

Try Set wsSheet = tempfile1.Worksheets(range("n2")).Value
 
Upvote 0
How about:


Dim bookName As String
bookName = Range("D2")
Dim shtName As String
shtName = Range("N2")
Dim tempfile1 As Workbook
Dim wsSheet As Worksheet
Set tempfile1 = Workbooks.Open(bookName, ReadOnly, notify = False)
Set wsSheet = tempfile1.Worksheets(shtName)
MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)
 
Last edited:
Upvote 0
How about:


Dim bookName As String
bookName = Range("D2")
Dim shtName As String
shtName = Range("N2")
Dim tempfile1 As Workbook
Dim wsSheet As Worksheet
Set tempfile1 = Workbooks.Open(bookName, ReadOnly, notify = False)
Set wsSheet = tempfile1.Worksheets(shtName)
MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)

grrrrr, this should work but it still fails at the Set wsSheet point.
 
Upvote 0
One more try:

Code:
Dim bookName As String
    bookName = Sheet1.Range("D2").Text
    Dim shtName As String
    shtName = Sheet1.Range("N2").Text

    Dim tempfile1 As Workbook
    Dim wsSheet As Worksheet
    Set tempfile1 = Workbooks.Open(Filename:=bookName, ReadOnly:=True, Notify:=False)
    Set wsSheet = tempfile1.Worksheets(shtName)
    MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)


Tell me I got it :)
 
Upvote 0
One more try:

Code:
Dim bookName As String
    bookName = Sheet1.Range("D2").Text
    Dim shtName As String
    shtName = Sheet1.Range("N2").Text
 
    Dim tempfile1 As Workbook
    Dim wsSheet As Worksheet
    Set tempfile1 = Workbooks.Open(Filename:=bookName, ReadOnly:=True, Notify:=False)
    Set wsSheet = tempfile1.Worksheets(shtName)
    MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)


Tell me I got it :)

Nope this still fails at the Set wsSheet = tempfile1.Worksheets(shtName)

This really makes no sense. The code logic is sound. Thanks for trying.
 
Upvote 0
Interesting. I tested the same code to be sure, and it did work. What if you hard code the values like this?

Code:
    Dim bookName As String
    bookName = "C:\MyBook.xlsx"
    Dim shtName As String
    shtName = "Sheet2"
 

    Dim tempfile1 As Workbook
    Dim wsSheet As Worksheet
    Set tempfile1 = Workbooks.Open(Filename:=bookName, ReadOnly:=True, Notify:=False)
    Set wsSheet = tempfile1.Worksheets(shtName)
    MsgBox tempfile1.Worksheets(wsSheet).Cells(14, 1)
 
Upvote 0
Changed it but still failing at the same point. I'm about to force the end-user to utilize sheets(1) though now it won't be as dynamic.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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