How do I assign a macro to a hyperlink?
You can do it using the Worksheet_FollowHyperlink event.
For example I recorded a macro named Macro1 and the following code will run the macro whenever the hyperlink is clicked
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Run ("Macro1")
End Sub
But this is not a very effective solution. My hyperlinks points to the same sheet (an by default to the 1st cell) so when ever the hyperlink is clicked the first cell in this sheet will be selected automatically.
I didn't investigate more on this. you can simply cancel the navigation (don't know if possible) or set the hyperlink property to the current cell so that the selection stays in the same cell.
Related
Goals
I am trying to make a macro that will copy the active cell and paste the contents of that cell to a text box.
I also need it to clear the text box before adding the new value, as well as keeping the original hyperlink source.
Context
I have a large list of items that have hyperlinks to various web pages. What I ultimately want is for a user to search a specific item, using the Ctrl + F function, and the item be copied with its hyperlink into a textbox at the top of the sheet.
Then once a new Item is searched it will clear the textbox and copy the new item into it.
Code
All it does is copy the active cell into the text box. I will not clear the box beforehand or copy the hyperlink.
Option Explicit
Private Sub TextBox1_Change()
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Copy
Sheet1.TextBox1.Paste
End Sub
If you're after the hyperlink address, in the text box then just use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet1.TextBox1 = Target.Hyperlinks(1).Address
End Sub
If you're after the text, but want the hyperlink to fire when the button (or text) is pressed - I'm not sure if either are possible. I guess perhaps you could use the OnAction to cause a hyperlink to fire.. but we'd need more info. Is this an ActiveX Textbox on a sheet?
I'd like to know on how to code a userform (VBA Excel) with automatically updating itself when the cell values has changed.
I have produced a button that will show the userform with labels and text boxes. But whenever i click it yes it shows up but i need to click the userform in order for me to see the values.
Need help.
Thank you in advance,
Tramyer
The Worksheet.Change event is fired everytime you change the contents of a cell on a given worksheet.
You can create an event handler in the worksheet module (usually labeled Sheet1, Sheet2 etc. in the VBA editor) that is called every time the event is fired:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
This example just outputs the address of the cell that was changed, but you can adapt this to update your userform with the changed values instead.
I have an Excel ListBox (not ActiveX as these are causing display issues) with an Excel (dialog menu-driven) macro that outputs its value to a named range cell on a different sheet to the Listbox. In that sheet's code I have the below
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print "ping"
End Sub
The macro event does not trigger when I click on the Listbox (therefore updating the named range cell value). I have verified that the macro executes when I directly update that sheet.
I assume the form control macro is circumnavigating the sheet event trigger. Am I right? Does anybody know an efficient workaround for this? I am stumped.
Thanks Kindly
You can assign a macro to the ListBox as ListBox1_Change event (right click › assign macro) which will be executed on change of the ListBox value instead of Worksheet_Change then.
Probably that is why the Worksheet_Change is not triggered anymore when using the ListBox to change the cell value.
I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub
I tried running the private sub code:
Private Sub Worksheet_Calculate()
Dim target As Range
Set target = Range("G3")
If Not Intersect(target, Range("G3")) Is Nothing Then
End If
End Sub
I am pulling a value from one sheet and putting it into another cell on a separate sheet. I want the VBA to run when the cell is automatically changed. When I tried the code above, nothing happened when I updated the cell.
From the sounds of things the issue seems to be that you're trying to put the code in a Module like a regular macro. To have it run based on a worksheet event, you need to have the code in that worksheet's code window (in the VBA window, there's the "Microsoft Excel Objects" folder, inside is the list of worksheets, double-click the worksheet to open it's code).
Once you've opened the worksheet's code, at the top of the window you should see two drop-downs. The left one should show "(General)". In that drop-down select "Worksheet" (should be the only option).
In the drop-down to the right of that, select "Change". Then you need to validate the Target is the right cell (If Target.Address = "$<Column letter>$<row #>"). Inside that If statement is where you'd nest your code to copy the value to the second worksheet