I'm generating lines and changes and want to automate the linking with a macro
However i wish to add the link to the macro in a clickable cell.
I tried using the same way i would link a URL, to get started then i need to figure out if
i can assign the "SubAddress:=" to a macro instead of a link?
ws.Range("H6").Hyperlinks.Add anchor:=ws.Range("H6"), Address:="", SubAddress:="runMACRO", TextToDisplay:="Show tasks"
Update SOLVED --
the solution i went with was link my macro as "screentip" then following this to run it ;)
Sub Worksheet_FollowHyperlink(ByVal target As Hyperlink)
Application.Run target.ScreenTip
End Sub
Yes, you can, but not in that way... A trick must be used. Making a hyperlink for the cell itself and then using the WorksheetFollowHyperlink event for the Target.Parent.Address. Look here, please...
As stated in the comments, you can make use of the Workbook_SheetFollowHyperlink event handler. Hence, you would have to, in the ThisWorkbook module, to add something like the following code which would call runMACRO whenever a hyperlink in cell H6 is clicked.
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
If Target.Range.Address = "$H$6" Then
Call runMACRO
End If
End Sub
Related
I'm trying to modify data in a cell range when changing the selection in a slicer.
Can this be done in Excel for Mac?
Yes, it is! As #BigBen had already mentioned, it is possible via the event Worksheet_PivotTableUpdate. This is also supported by Excel for Mac.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
End Sub
It is important that the code is defined in the corresponding sheet module
I want to call/ activate a button at the end of a Sub. I know the cell address of the command button, but I don't know the name/ID of the button.
How do I select/activate the button without looping through each button on active sheet?
https://stackoverflow.com/a/30600479/13049793
I have created my buttons on multiple rows with each assigned to the same macro, from my understanding, I cannot call the macro the button is assigned to because the macro uses the button's relative position, below is a simple example to illustrate the use of the buttons relative position:
Sub ExampleButtonClick()
Dim Cellvalue As String
Cellvalue = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, -1).Value
Msgbox (Cellvalue)
End Sub
i assume you want to run one macro that at the end initiates a different macro, i also assume the reason the button is rather not as a sub that is just called is because it has its independent function that can be used without the other sub
assuming that you used a command button as an activeX control, why not just use a private sub that the button performs and place that in an individual module and at the end of your current sub;
call module1 'assuming the private sub exists in module 1
or rather include the code even if duplicated in the first sub, searching for a button seems the circular route unless there is another intention you wish to pursue.
maybe elaborate on the task at hand or post the current code you are working on?
This macro works as intended, from a button on the Batch Input sheet:
Sub BatchTriggerOFF()
Sheets("Batch Input").Unprotect
Sheets("Batch Input").Range("G3:J3").Value = "Off"
Sheets("SQL LOGIC").Calculate
Sheets("Batch Input").Range("A12").Select
Sheets("Batch Input").Shapes.Range(Array("Group 12")).ZOrder msoSendToBack
Sheets("Batch Input").Protect
End Sub
However, when BatchTriggerOFF is called from a different sheet in the same workbook, the macro neither changes the Range("G3:J3").Value nor Shapes.Range(Array("Group 12")).ZOrder msoSendToBack. There is no error message.
If Sheets("SQL LOGIC").Range("B1") = "On" Then Call BatchTriggerOFF
I've tried unprotecting the Batch Input sheet beforehand, messing with Sheets("Batch Input").Activate, Sheets("Batch Input").Select, and even tried pasting the BatchTriggerOFF line by line VBA directly into the second macro, to no avail.
What is causing BatchTriggerOFF to seemingly not run when called from the second macro/sheet?
[...] something is inherently wrong with the second code I've provided, likely not actively running when the value in Range("B1") is changed?
Exactly. A procedure that's in a standard module needs something, somewhere to invoke it. Could be a shape or button on the worksheet, could be other VBA code, but something needs to invoke it somehow.
No procedure is going to just know to run when Range("B1") is changed on Sheets("SQL LOGIC"): you need to have code that's "triggered" when a cell is changed on that sheet.
The way to do this, is to handle the worksheet module's Change event. Find your "SQL LOGIC" sheet in the VBE's Project Explorer (Ctrl+R), double-click it. In the code-behind module for that specific worksheet, select Worksheet from the left-side dropdown at the top of the code pane; the right-side dropdown should say SelectionChange, and the VBE should have added a private procedure that looks like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Select Change from the right-side dropdown; the VBE creates a private procedure that looks like this:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Now delete the SelectionChange handler, you don't need it unless you need to track cells that the user has selected. Since we want to track cells that have changed, we'll use the Change worksheet event. This procedure will be invoked whenever the user or your code changes anything on that sheet.
Since we only care to run code when a specific cell is changed, we need a condition here, involving the Target parameter. Using the Application.Intersect function, we can get a Range object reference that's Nothing if the two specified ranges don't intersect; we can use this information to bail out if it's not B1 that changed:
If Application.Intersect(Me.Range("B1"), Target) Is Nothing Then Exit Sub
Any code written after that condition inside the Worksheet.Change event handler procedure, will only run after the value of cell B1 was modified - either by the user typing in a value, or by any other code writing to that cell (you need to toggle Application.EnableEvents off if you have to prevent firing that event when it's code doing the changes and you don't want the handler to run).
Now, it looks like cell B1 isn't going to change, rather, it looks like it contains a formula whose result might change after making changes to the "Batch Input" sheet.
If that's the case, then the Change event will not be fired when B1 recalculates and now evaluates to a new value, because the cell didn't change, only its result.
If that's your scenario, then you want to handle the Calculate worksheet event, and have that be your trigger:
Private Sub Worksheet_Calculate()
If Me.Range("B1").Value = "On" Then BatchTriggerOFF
End Sub
If you need your sub to be called from any (sheet) module, move it in a module! The function/sub in the sheet module cannot be called without specifying the module name where it belongs, like you will be able to do in a module.
I have a large chunk of VBA code that generates a result sheet. It's fairly large, and in order to better, faster dig through it, I've added a dynamic hyperlink at the top of the sheet, with a drop-down menu next to it. Drop down the item, click the hyperlink, and you get whooshed over to the part of the spreadsheet you want to get to.
I've been asked to make it even easier, and when you select an item from the drop down menu, to auto-whoosh you to the correct location. So an on-trigger macro to click a dynamic hyperlink.
Ok, so far, so good. Do some googling, and I end up with the following:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("HyperlinkType")) Is Nothing Then ClickHyperlink
End Sub
Private Sub ClickHyperlink()
ThisWorkbook.Names("HyperLinkTotal").RefersToRange.Hyperlinks(1).Follow
End Sub
Unfortunately, this results in a subscript out of range, which apparently can happen with dynamic hyperlinks.
The hyperlink formula for reference:
=IFERROR(HYPERLINK("#Totals!B"&MATCH(HyperlinkType,B:B,0),"Jump to "&HyperlinkType),"Please enter a valid type")
1) How do I fix the subscript out of range issue?
2) Is there a better way than hyperlink(1)? It almost looks to me like it's indexing the hyperlink, and I'm not sure that's exactly what I'm looking for - I'm looking for the hyperlink in the cell, not the first in the workbook. I may be misunderstanding.
Previous instances of this, and similar question on stack overflow:
Excel Macro executing Hyperlink shows 'Subscript out of range error' - no answer
Hyperlinks.Follow error: Run-time error '9': Subscript out of range - completely different method used to solve that particular issue (XY problem)
Hyperlink code shows Subscript out of range error vba excel - used a reserved word as a variable
VBA to open Excel hyperlink does not work when hyperlink generated with a formula - Seems to be promising, I think this might solve it.
Thanks to #Forward Ed, I was able to get it working with select.
Forgive the lazy lack of variables:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("HyperlinkType")) Is Nothing Then ClickHyperlink (Me.Range("HyperlinkType").Value)
End Sub
Private Sub ClickHyperlink(ActuarialString As String)
Dim ResultRow As Long
ResultRow = Me.Range("B:B").Find(ActuarialString).Row
Me.Cells(ResultRow, 2).Select
End Sub
To put it another way: If you want to click on a dynamic hyperlink, you're probably running into the XY problem. Step back, figure out exactly what you're trying to accomplish, and use one of VBA's other tools to do it.
i need to click a button(FormControl) on an excel sheet and run the macro assigned to it, through VBA code.
I tried what was suggested here
https://stackoverflow.com/questions/130166/clicking-command-button-from-other-workbook
but it didn't work for me.
Is there any other way to do this ??
Fairly easy method for doing this, even if it's not really documented.
CommandButton1.Value = True
Did you actually try? Because you want exaclty what is suggested there.
Application.Run Activesheet.Shapes(1).OnAction
If in a worksheet. I used this in the past and it worked!
Sheet1.CommandButton1_Click
else
Sheets("Sheet Name").CommandButton1_Click
In my case, I needed to add a button, say on Sheet1, to call the code of CommandButton1 on, say, Sheet2. First I had to delete the 'Private' statement that was in front of CommandButton1_Click(). Then I had to call Worksheets("Sheet2").Activate before calling Worksheets("Sheet2").CommandButton1_Click. Then it worked. You can then add Worksheets("Sheet1").Activate to get you back to your original sheet. So:
Private Sub CommandButton1_Click()
Worksheets("Sheet2").Activate
Worksheets("Sheet2").CommandButton1_Click
Worksheets("Sheet1").Activate
End Sub