ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Macro to open worksheet based on name in cell (https://www.excelbanter.com/excel-discussion-misc-queries/222353-macro-open-worksheet-based-name-cell.html)

Sunnyskies

Macro to open worksheet based on name in cell
 
I need a macro that will open a worksheet based on the worksheet name in a
cell.
I have four worksheets:-COC, BVO, Vehicles and Reports
Now on the Main sheet in cell B2 is a formula which based on criteria will
vlookup either COC or BVO or Vehicle or Reports.
Once the name appears in B2 I want that applicable sheet to be viewed.


Stefi

Macro to open worksheet based on name in cell
 
Try something like this:
Sub test()
filetoopen = ThisWorkbook.Worksheets("Main").Range("B2")
Workbooks.Open Filename:=filetoopen
End Sub

Make sure that B2 contains full file name e.g.
C:\pathname\COC.xls

Regards,
Stefi


€˛Sunnyskies€¯ ezt Ć*rta:

I need a macro that will open a worksheet based on the worksheet name in a
cell.
I have four worksheets:-COC, BVO, Vehicles and Reports
Now on the Main sheet in cell B2 is a formula which based on criteria will
vlookup either COC or BVO or Vehicle or Reports.
Once the name appears in B2 I want that applicable sheet to be viewed.


Gary''s Student

Macro to open worksheet based on name in cell
 
Something like this event macro:

Private Sub Worksheet_Calculate()
Set b2 = Range("B2")
Sheets(b2.Value).Activate
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200835


"Sunnyskies" wrote:

I need a macro that will open a worksheet based on the worksheet name in a
cell.
I have four worksheets:-COC, BVO, Vehicles and Reports
Now on the Main sheet in cell B2 is a formula which based on criteria will
vlookup either COC or BVO or Vehicle or Reports.
Once the name appears in B2 I want that applicable sheet to be viewed.


Stefi

Macro to open worksheet based on name in cell
 
Sorry, now I see that the OP wrote worksheet and not workbook!
Stefi


€˛Gary''s Student€¯ ezt Ć*rta:

Something like this event macro:

Private Sub Worksheet_Calculate()
Set b2 = Range("B2")
Sheets(b2.Value).Activate
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200835


"Sunnyskies" wrote:

I need a macro that will open a worksheet based on the worksheet name in a
cell.
I have four worksheets:-COC, BVO, Vehicles and Reports
Now on the Main sheet in cell B2 is a formula which based on criteria will
vlookup either COC or BVO or Vehicle or Reports.
Once the name appears in B2 I want that applicable sheet to be viewed.


Gord Dibben

Macro to open worksheet based on name in cell
 
Private Sub Worksheet_Calculate()
On Error GoTo endit
Application.EnableEvents = False
With Me.Range("B2")
If .Value < "" Then
Select Case .Value
Case "COC"
Sheets("COC").Select
Case "BVO"
Sheets("BVO").Select
'more Cases here
End Select
End If
End With
endit:
Application.EnableEvents = True
End Sub

Right-click on the "Main" sheet tab and "View Code". Copy/paste the above
into that sheet module.

Edit to suit..........add more Cases..........then Alt + q to return to the
Excel window.


Gord Dibben MS Excel MVP


On Thu, 26 Feb 2009 00:33:01 -0800, Sunnyskies
wrote:

I need a macro that will open a worksheet based on the worksheet name in a
cell.
I have four worksheets:-COC, BVO, Vehicles and Reports
Now on the Main sheet in cell B2 is a formula which based on criteria will
vlookup either COC or BVO or Vehicle or Reports.
Once the name appears in B2 I want that applicable sheet to be viewed.



Gord Dibben

Macro to open worksheet based on name in cell
 
Now why didn't I think of that instead of the Select Case effort I posted?

Maybe because I'm losing it?<g


Gord


On Thu, 26 Feb 2009 02:12:06 -0800, Gary''s Student
wrote:

Something like this event macro:

Private Sub Worksheet_Calculate()
Set b2 = Range("B2")
Sheets(b2.Value).Activate
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm




All times are GMT +1. The time now is 08:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com