Macro to open worksheet based on name in cell

S

Sunnyskies

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.
 
S

Stefi

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:
 
G

Gary''s Student

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
 
S

Stefi

Sorry, now I see that the OP wrote worksheet and not workbook!
Stefi


„Gary''s Student†ezt írta:
 
G

Gord Dibben

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
 
G

Gord Dibben

Now why didn't I think of that instead of the Select Case effort I posted?

Maybe because I'm losing it?<g>


Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top