Reputation: 191
How do i activate the worksheet of the workbook that i have already opened previously based on cell text from my current workbook? For instance, I tried to use the code below to activate the worksheet deviceB in "Book1" where i have already opened from the current workbook that i am using called "anotherworkbook" but it says subscript out of range when clearly the worksheet device B exists in "Book1".
ThisWorkbook.Worksheets(Sheets(1).Range("A2").Text).Activate
'trying to activate the worksheet device B in "Book1" from another workbook that i am currently using called "anotherworkbook"
Workbook called book1 that i have already opened where i am trying to activate the worksheet lets say device B based on cell text from my current workbook called "anotherworkbook"
Current workbook that i am using called "anotherworkbook"
Upvotes: 0
Views: 939
Reputation: 96753
First Activate the workbook.
Then Activate the worksheet on that workbook.
(Then Activate the Range on that worksheet, if desired)
EDIT#1:
Here is some working code. Assumes:
ThisWorkbook
and contains the macrosBook1.xlsx has a single sheet and in cell A2 of that sheet is the text Device B
Sub Routine()
Dim databook As Workbook, s As String
Set databook = Workbooks.Open(Filename:="C:\TestFolder\Book1.xlsx")
s = databook.Sheets(1).Range("A2").Text
ThisWorkbook.Activate
Worksheets(s).Activate
End Sub
Upvotes: 1