Macro to detect clicking on a cell in a specific row - excel

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

Related

How can i fix the run-time error '91' in excel VBA?

Im trying to trigger a popup msg for column q if it differs from "Open" Or "Closed" which say Please enter Justification this code works well but it gives me the bellow error message if i click on a different column (other than Q)
how can i get rid of the message?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Open") And Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Closed") Then
MsgBox "Please Enter Justification"
End If
End Sub
One way is to just check the .Column of the Target to make sure it's Q first.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 17 Then
If Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Open") And Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Closed") Then
MsgBox "Please Enter Justification"
End If
End If
End Sub
There are probably much cooler ways.

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

VBA Code to autohighlight modified cells?

Currently using this code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "$B$1" Then Range("B9:AE53").Interior.Color = xlNone
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("B9:AE53")) Is Nothing Then
For Each c In Intersect(Target, Range("B9:AE53"))
Target.Interior.Color = vbYellow
Next c
End If
End Sub
Autohighlight only works when I manually edit (or F2 then enter) the cells in B9:AE53. I was hoping for something that would change the cell color if I edit the data in the orders sheet (reference for B9:AE53).
Was also hoping to transfer the event from B1 to a command button.
You can easily change the second part of your question, the one where you wish to reset the cell colors to nothing, to be executed by a button.
Change the code to this:
Sub CleanUp()
Range("B9:AE53").Interior.Color = xlNone
End Sub
Then add a button via the Forms Control Button in the developer toolbar and asign the above macro to it. This will clean up the area. Once you specifiy what the desired outcome is for the first part of you question I might be able to help with that.

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. :)

Macro to run when cell value changes

I am realy not sure what the problem with this code is or if it is a problem with the formatting of my worksheet but the following code will not automatically run when the value of D8 changes:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub
Is it a formating issue or a code issue?
After seeing your question Here
I'd like you to try this:
In the work book goto the sheet that you wish to change the value of D8 and have the code run.
At the bottom of Excel right click the Tab with the name of that sheet and then select View Code
At the top of your Code window you should see the word "(General)".
Click on the drop down and select "WorkSheet", you should see a new Sub called
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You can delete those lines. Now to the right of where you selected Worksheet theres another drop down this one should say "SelectionChange" Click on that then Select the word "Change" from the drop down. You should see another new sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
In between those 2 lines is where you want
If Target.Address = "$D$8" Then
Toggle_Rows
End If
Not an answer but a way to debug. as this works for me both manually and via code.
Try one of the following to see the value you get:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
Debug.Print Target.Address
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub

Resources