VBA to run various macros when clicking on different cells in Excel - 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

Related

Auto expansion of drop-list in merged cells in excel

I have some merged cells with data validation drop down lists I tried the following method to make them automatically expand when on focus it applies on single cells, but when I tried to apply on merged cells, writing their range it doesn't work.
On focus after tabbing of Excel drop down, automatically show list for selection
first code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target = Range("d10") Then
Application.SendKeys ("%{UP}")
End If Err1:
'do nothing End Sub
[it worked perfectly][1]
when I merged cells it doesn't work, even I tried to change range to all merged cell it doesn't work either.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target = Range("d10:n10") Then
Application.SendKeys ("%{UP}")
End If Err1: 'do nothing End Sub
Any idea how make it work?
Thanks in advance.
Merged cells lead to all kinds of problems and should be avoided. You could use
If Target.Address = "$A$1:$C$3" Then
or better yet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
MsgBox "merged cells are bad for your health"
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

Why does a macro assigned to a cell fail?

I have a macro assigned to a cell that executes once clicked on. The macro involves SAP scripting, which if ever interrupted (via CTRL+DEL+ALT), also causes my previously clickable cell to stop working.
I can run the macro via Developer yet the cell is not working. Note that the code provided below is pasted in the sheet where the clickable cell is, not in ThisWorkbook.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal target As Range)
If Selection.CountLarge = 1 Then
If Not Intersect(target, Range("A10")) Is Nothing Then
Call FolderPicker
End If
End Sub
Any ideas what might be causing the issue?
You need to close all if statements.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal target As Range)
If Selection.CountLarge = 1 Then
If Not Intersect(target, Range("A10")) Is Nothing Then
Call FolderPicker
End If
End If
End Sub
You could use:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check count how many cells affected to avoid errors
If Target.Count = 1 Then
If Not Intersect(Target, Range("A10")) Is Nothing Then
Application.EnableEvents = False 'Disable events to avoid pointless code trigger
Call FolderPicker
Application.EnableEvents = True
End If
End If
End Sub

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

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

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