Forum Discussion
table of contents
- Apr 03, 2018
Lorenzo you don't need VBA to do what you require.
Simply Right Click in Cell C3 > Link > Insert Link > Place In This Document > Select the sheet you want to navigate to.
If you want VBA this will do the trick.... put this code in the TOC code Module:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Select Case Target.Address Case "$C$3": Sheets("Sheet1").Activate Case "$C$4": Sheets("Sheet2").Activate Case "$C$5": Sheets("Sheet3").Activate Case "$C$6": Sheets("Sheet4").Activate End Select End Sub
Mr Matt Mickle
Thank you for your quick reply
the vba worked nicely - but can you please insert this:
when I press X at the upper right corner of the sheet - it will not close the workbook but will return to the first sheet (TOC) and in that TOC sheet is the control for exiting (closing) the file (say on cell c9)
that is - all other sheets has no provision to close the file except to return to sheet TOC
Many thanks
Lorenzo-
Try these modifications:
TOC Worksheet Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Address
Case "$C$3": Sheets("Sheet1").Activate
Case "$C$4": Sheets("Sheet2").Activate
Case "$C$5": Sheets("Sheet3").Activate
Case "$C$6": Sheets("Sheet4").Activate
Case "$C$9": Call CloseFile
End Select
End Sub
Workbook Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True 'Cancel Workbook Close
Sheets("TOC").Activate
End Sub
Code Module (Insert a new code module):
Sub CloseFile()
Dim Answer As String
Dim MyNote As String
'Supress Save MessageBox
Application.DisplayAlerts = False
Application.EnableEvents = False
'Place your text here
MyNote = "If you would like to save the file before closing it please click YES, otherwise click NO."
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNoCancel, "???")
If Answer = vbNo Then
'Code for No buttton press
ThisWorkbook.Close False
ElseIf Answer = vbYes Then
'Code for Yes button Press
ThisWorkbook.Close True
Else
'Code for Cancel button Press
Sheets("TOC").Activate
End If
Application.EnableEvents = True
End Sub
- Lorenzo KimApr 04, 2018Bronze Contributor
Mr Matt Mickle
one small thing
I noticed that the vba makes the program close without exiting excel
how should the code be changed such that after saving or not (excluding cancel - which return to TOC) the program to exit excel?
thanks again
- Matt MickleApr 04, 2018Bronze Contributor
Application.Quit will exit the application. However, if you close the workbook (ThisWorkbook.Close) this line will not execute ( Application.Quit) .....It's a kind of a catch 22. You may need to decide what is more important to you and then adjust the code accordingly.... If you adjust the workbook close event to always prompt a question it could fix your dilemma... The reason it's tricky is because one event activates another...etc. Thus the large amount of code....to handle for these scenarios.
- Lorenzo KimApr 06, 2018Bronze Contributor
OneDrive (file://RNDSTN1/Users/LFKIM/OneDrive)
I think the above is the link for you to access the file (hope so)
thank you
- Lorenzo KimApr 04, 2018Bronze Contributor
Mr. Matt Mickle
your macros worked like a charm!!
this will be most helpful in my next project
THANK YOU VERY MUCH FOR YOUR KIND ASSISTANCE
MORE POWER TOYOU