Trigger VBA script when any 1 cell selected containing a defined string - excel

The code below worked fine so long as the "MainSubroutine" only needed to be triggered when cell G14 was clicked on, but now I need to widen the trigger event to whenever any target cell on the sheet is clicked on and it has the specific string, "Click to Learn More".
So how do I write that? I tried altering line 2 to "If Selection.Count = 1 AND selection.value = "Click to Learn More" Then..." but obviously that didn't work.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("G14")) Is Nothing Then
Call MainSubroutine
End If
End If
End Sub

This should do it? If it can contain the string use Instr or similar.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If target.Count = 1 Then
If Target.value="Click to learn more" Then
Call MainSubroutine
End If
End If
End Sub

Related

VBA to run various macros when clicking on different cells in Excel

Good day
I've come across VBA code that works really well to trigger a macro in Excel when clicking on a specific cell. See below:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
Call MyMacro
End If
End If
End Sub
I'm trying to add 3 more such scenarios to the same code, but I have no experience with coding, so troubleshooting has gotten me nowhere. Please see below example of what I'm trying to do, and correct the code if possible:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
Call MyMacro1
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E10")) Is Nothing Then
Call MyMacro2
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("G23")) Is Nothing Then
Call MyMacro3
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("J33")) Is Nothing Then
Call MyMacro4
End If
End If
End Sub
Any assistance will be greatly appreciated!
I was expecting the various macros I created (that work well) to automatically run when the cells noted in the code were clicked on
Something like this using Select Case might be easier to manage.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Address(False, False)
Case "D4": MyMacro1
Case "E10": MyMacro2
Case "G23": MyMacro3
Case "J33": MyMacro4
End Select
End Sub

VBA script or something else to automatically pushes a button

Is it possible to make a VBA script or something else that automatically pushes a button with a macro in it?
For example, if the cell (1:I) has WAAR in it, that the button (planning maken) is pushed automatically (see image)?
It doesn't have to be this way, if you know something else it's fine too.
No, it is not possible to "push a button" automatically.
However, I imagine that the code behind the "push button" is something like:
Sub CommandButton_Click(Target as Range)
...
End Sub
What you can do, is create a macro that launches this procedure, but then you have the problem, when would anybody launch that macro? There, you have provided the answer (more or less) yourself: when cell "I1" gets a certain value. This, however, is not possible: you cannot declare a macro to be launched when a cell gets a certain value.
But: you can launch a macro when any cell is changed, it works as follows:
What does this all mean?
In the VBA project editor (left pane), you need to select your sheet, and in the source code editor, you need to go for "Worksheet" and "Change", like this you have the event which is called whenever some value changes in your worksheet.
The code itself looks as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
CommandButton_Click (...)
End If
End Sub
This means that the macro will always be launched, but you only want something to happen when:
Target.Row = 1 (which correspond with cell "I1")
Target.Column = 9 (which corresponds with cell "I1")
Target.Value = True (which corresponds with value `WAAR`, I assume here that `WAAR` is just
the Dutch translation for the boolean `True` and not some string value)
You can use Worksheet_Change event to trigger a macro. So, you have to make sub and call it from Worksheet_Change event. Sub will be same as Planning Button doing. Check below code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("I1")) Is Nothing Then
If Target = "WAAR" Then
Call MyMacro
End If
End If
End Sub
Sub MyMacro()
MsgBox "Value is entered to I1 cell"
End Sub
Depending on which type of button it is you need to either call the Sub the button is connected to, or trigger the button_click event procedure.
Say you have a Forms button which is calling the Sub Test:
Public Sub Test()
MsgBox "Forms Button clicked"
End Sub
In your Worksheet_Change() procedure your code would look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
Test
End If
End Sub
If you have an ActiveX button your code would look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
Sheet1.CommandButton1.Value = True ' This triggers the click event
End If
End Sub

One macro running , the other does not

I am importing currency data from a website on a click event.
The import works and its called by this code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F13")) Is Nothing Then
Call GetCurrency
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F14")) Is Nothing Then
Call UpdateCurrency
End If
End If
End Sub
If the cell F13 is clicked, the GetCurrency macro runs, imports the data, wonderful.
But clicking F14 causes nothing.
The update currency macro looks like this
Sub UpdateCurrency()
Range("N15").Value = Range("I19").Value
Range("N14").Value = Range("I26").Value
Range("N16").Value = Range("I22").Value
End Sub
This should just update some other cells in order to make another formula work properly. Question is, why does clicking the cell F14 not run the UpdateCurrency function?
It think you need to change the if statements a bit.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Target = Range("F13") Then
If Not Intersect(Target, Range("F13")) Is Nothing Then
Call GetCurrency
End If
Else
If Not Intersect(Target, Range("F14")) Is Nothing Then
Call UpdateCurrency
End If
End If
End If
End Sub

Macro to detect clicking on a cell in a specific row

I'm trying to figure out how to get a macro to run if I click on any cell in a specific row in Excel.
Let's say I want to show a message "hello world" if I click on any cell in row 4. I tried looking around, but only got as far as the follows:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("4")) Then
MsgBox "Hello World"
End If
End If
End Sub
Obviously this is wrong, but any help in the right direction would be appreciated. Thanks.
This should work:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row = 4 Then
MsgBox "Hello World"
End If
End Sub

Excel to voice cells' value whenever it is selected with VBA

I'm triyin to achieve that Excel "says" the value of a cell when placing on it.
I've got this code but i'm able to hear cells' value only after i select another one. Can you help me?
Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = ActiveCell
Application.Speech.Speak (Target)
End Sub
If you want Excel to "say" whatever you enter in a cell, first run this short macro, then make entries:
Sub WhatDidYouEnter()
Application.Speech.SpeakCellOnEnter = True
End Sub
and if you want Excel to "say" whatever you have Selected, then the Event Macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveCell
If .Value <> "" Then Application.Speech.Speak (.Value)
End With
End Sub
will do the trick.
Try this out:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Debug.Print Target.Address
End Sub
Then change it to speak, and not to debug. :)

Resources