Need Help with a Macro to activate a specific sheet: Excel 2012

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello all,

I have a macro I am working (see below). I have defined "varCellvalue" as a name of a Excel file in a cell which is a drop down list and "VarCell" as a Sheet name in every file of the drop down list so what I am trying to accomplish is that whatever a user selects as a file and a tab name the macro will open a specific file and specific sheet.

Currently the file will open but the specific sheet isn't working? any help or recode to help this would be greatly appreciated. Thanks all!!


Sub OpenWorkbook()

varCellvalue = Sheets("Main").Range("B23").Value

varCell = Sheets("Main").Range("B19").Value

If Not IsEmpty(Range("B23").Value) Then

Workbooks.Open "\\Lax-Netapp01\Dept_private\Business Systems\LFC MACRO TEST\" & varCellvalue & ".xlsm"
Sheets(" & VarCell & ").Activate

Else

Workbooks.Open "\\Lax-Netapp01\Dept_private\Business Systems\LFC MACRO TEST\" & varCellvalue & ".xls" Sheets(" & VarCell & ").Activate

End If

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Someone probably has a better idea. But couldnt you simply have the macro open the workbooks (like you have). Then have another line of code activate the sheet once open?

Activeworkbook.Sheets(VarCell).Activate

Has this after your open command. I might go quickly check.

EDIT:

Yep worked for me:

Code:
Workbooks.Open "\\Lax-Netapp01\Dept_private\Business Systems\LFC MACRO TEST\" & varCellvalue & ".xls"

ActiveWorkbook.Sheets(VarCell).Activate

This modification should work.
 
Last edited:
Upvote 0
Just adding this "Activeworkbook.Sheets(VarCell).Activate" worked perfect for me! Thanks again!

:)
 
Upvote 0
No worries.

It might be more accurate however to have this:

Workbooks(varCellvalue & ".xls").Sheets(VarCell).Activate

Instead, as it removes the "activeworkbook" thing and is more specific, might stop some potential issues.
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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