0
votes

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"

enter image description here

Current workbook that i am using called "anotherworkbook"

enter image description here

1

1 Answers

1
votes

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:

  1. master.xlsm is ThisWorkbook and contains the macros
  2. master.xlsm has a worksheet named Device B
  3. Book1.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