Excel Hyperlinks and VBA - vba

I have a Data Validation list
which refer to my various worksheets
I would like to be able to select the worksheet I wish to view / jump to and use a VBA button to take me to that worksheet.
Is this possible please?
PS: I would like a single button to go to the sheet selected from the dropdown

In the VBA Project window double click the Worksheet that has your validation list. This will open the that Worksheet's code module. Paste this code and change Range("E1") to the correct cell address.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Taget, Range("E1")) Is Nothing Then
On Error Resume Next
Sheets(Range("E1").Text).Select
On Error GoTo 0
End If
End Sub

Related

Run VBA when cell changes on a different worksheet

I am very new at VBA, but have managed to make a code which links the header of my worksheet to a cell on another sheet.
The code runs every time I click a cell in the active worksheet. I'd like for the code to only run when a certain cell in the other worksheet changes.
"Report" has the header
"Input" has the cells which the code refers to (B18) and the cell I want the code to run on when changed (B3).
The following is the code that I already have.
Private Sub Worksheet_selectionChange(ByVal Target As Range)
ActiveSheet.PageSetup.RightHeader = "&28" & Format(Worksheets("Input").Range("b18").Text)
End Sub
Any help is much appreciated!!!
Not sure about your second paragraph; my understanding is that you want to monitor cell B3 on the Input worksheet, and when it changes, adjust the RightHeader of the PageSetup of worksheet Report so it reflects what's in cell B18 on the Input worksheet. If that's correct, you could put the following code behind the Input worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("B3")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("B18").Text
End If
End Sub
...and remove your Worksheet_SelectionChange procedure.
The idea is to monitor for changes on the Input worksheet, and act upon those involving its B3 cell.
Ideally, you'd give names to the cells above, so that your code would continue working even if you add or delete rows and columns on the Input worksheet. See Define and use names in formulas. If you were to define Inputs!B3 as MyMonitoredValue and Inputs!B18 as MyPageSetupValue, the code above would become:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...and you could re-organize the layout of the Input worksheet without worries.
Finally, instead of referring to worksheets by their tab's name (as seen from Excel), you can refer to them directly using their CodeName, as seen from the VBA editor, in the Properties window, under the worksheet's (Name) property (press Ctrl+R to show the Project Explorer, click on the worksheet under the Microsoft Excel Objects node, then press F4 to display the Properties window). You can change this value; I'd typically change it to shtReport. Yet another version of the code would then be:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
shtReport.PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...which is able to withstand both changes in the Report worksheet's tab name, and reorganization of the Input worksheet.

Excel VBA DoubleClick

I'm working on a work project where I have an excel sheet with values that turn red when they are out of spec. What I'd like to do is be able to double click on a cell and have the sheet in my workbook pop up that has trending data on it. I have already created the sheet with the graph on it. Long story short, I'd like to be able to double click on a specific cell and have it bring up the corresponding sheet.
I have tried this code, and it will not work. Is anyone able to maybe write code from scratch or alter the code so I could use it? The cell I'm trying to click on is N9, and the sheet I want it to open is called "Alpha Final Rinse"
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
Sheets("Alpha Final Rinse").Select
End Sub
I'm doing this in Excel 2013. Thank you!
If you only want N9 to be able to switch focus to another worksheet, isolate Target with the Intersect method.
In the data worksheet's code sheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
If Not Intersect(Target, Range("N9")) Is Nothing Then
cancel = True
Worksheets("Alpha Final Rinse").Activate
End If
End Sub
Note that cancel = True is necessary to stop the user entering in-cell edit mode (assuming that has been enabled in Options).
Your code will work if:
it is installed in the worksheet code area of your data sheet (the sheet whose cells you are double-clicking)
macros are enabled
the file type is .xlsm rather than .xlsx

Dialog box that selects a worksheet in Excel

I'm trying to build a macro that will duplicate a worksheet from one workbook into a worksheet in another workbook. Is there a way I can use VBA code to allow me to manually select which worksheet I shall be duplicating?
Right now the macro works, as long as I have the full worksheet name typed into the actual VBA code. Ideally, I'd like the macro to allow me to select the worksheet through a dialog box. I know you can just copy/paste the sheet or its contents, but the guys I'm working for don't want to do that, due to the size.
You can use Worksheets collection to populate the ListBox in a user form. This should get you started:
Code in the user form (!):
Private Sub UserForm_Initialize()
Dim v As Worksheet
For Each v In Worksheets
UserForm1.lstWorksheets.AddItem v.Name
Next
End Sub
Private Sub cmdSelectWorksheet_Click()
MsgBox "You selected " & lstWorksheets.Value
End Sub

VBA - trigger macro when radio button is selected

I am having trouble running a macro automatically when I select a radio button option. There are two radio-button options, which are linked to the cell named "SimType" on the sheet codenamed "MAIN". I have adapted the following code from MSDN and have included it in the module for the MAIN worksheet but cannot get it to function:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("SimType")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
RefreshSimsList
End If
End Sub
I understand Target refers to the ActiveCell so when I manually enter values of 1 or 2 into Range("SimType"), the macro triggers. However, when the cell is altered by the linked radio-buttons, no action is triggered. What can I do to have the sheet recognize that the value has been changed without it being the ActiveCell?
Place this code in a module
Sub OptCommon()
'
'~~> Rest of the code
'
End Sub
And simply right click on both the option button and assign macro to it. Now not only the cell value will change but the macro will also run.

How can I run a Macro when the value in a specific cell changes?

Let me preface by saying that I am very new to VB...
I am trying to run a macro whenever the value in a certain cell changes. I've read up on how to do this, but can't seem to get it to work. I have entered the following code into the private module of the worksheet object:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
Application.Run _
"'Amex Payments_Experiment.xlsm'!SelectCells"
End If
End Sub
C5 is the cell I am trying to monitor for change.
"SelectCells" is the macro I want to run.
"Amex Payments_Experiment.xlsm" is the name of the file.
When I change the value in C5 nothing happens. Some help would be great. Thanks!
UPDATE:
Cyberkiwi - No, that is not exactly how I did it, but when I follow you're instructions I do find the code where you say it should be. To get to the private module of the worksheet object I right clicked the sheet tab at the bottom, selected "view code", then selected "worksheet" from the dropdown in the top center of the page.
User587834 - Yes. Macro's are enabled.
Any other suggestions?
if you use Excel2007 be sure that macros are enabled, by default Excel 2007 deactivate macro execution for new workbook.
for that try to execute any other macro to be sure that macros are enabled.
I have entered the following code into the private module of the worksheet object:
How exactly have you done that? As below?
Alt-F11 to switch to code view
On the left, double-click on the target sheet
On the right, do you see the code you entered? if not, proceed to next step
Paste the code block
This code works Ok for me
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("$C$5")) Is Nothing Then
MsgBox "hello"
End If
End Sub
Maybe the problem is with your Application.Run:
- select the line containing the intersect and click F9 to switch on Debug, then try changing a cell to see if you get to the sub.
If you never reach that line then you have not got the code in the worksheet clkass module, or you have switched off events, or macros are disabled or ...
Check the Application.EnableEvents property, and set it to True if required.