In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?
Found a solution:
Make a named range for the cells you want to capture clickevent
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.ActiveSheet.Cells(1, 1).Value = Target.Address
If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then
' do your stuff
Range("A1").Select
Endif
Application.EnableEvents = True
End Sub
You can bind to a cell double click.
Open VBA, goto the worksheet you want to wire up the event
Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right
The check the Target.Address is equal to the address of the cell you care about and call the function you wish.
Something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(True, True, xlA1) = "$A$1" Then
Call MyDemo
End If
End Sub
Private Sub MyDemo()
MsgBox "Hello"
End Sub
Related
I have simple macros for clearing cells on "Sheet1", which have drop down lists.
Sub reset1()
Range("D20:E21").ClearContents
Range("D8:E9").ClearContents
Range("D6:E7").ClearContents
End Sub
Sub reset2()
Range("D20:E21").ClearContents
Range("D8:E9").ClearContents
End Sub
Then I call these macros on "Sheet1" if the cell values change
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$4" Then
Call reset1
End If
If Target.Address = "$D$6" Then
Call reset2
End If
End Sub
This code is written on the "Sheet1".
Normally it works but sometimes reset1() doesn't work.
I should then save and reopen the excel or run the macro manually.
Should I better modify some codes?
First problem is that with Range("D20:E21") it is not clear in which worksheet that range should be. Always specify the worksheet like Worksheets("Sheet1").Range("D20:E21").
Second problem is that if you .ClearContents in a Worksheet_Change event this is a cell change and triggers another Worksheet_Change event and so on. So it is recommended to disable events Application.EnableEvents = False before changing cells in Worksheet_Change event.
Third problem is that if you test Target.Address = "$D$4" and you copy paste a range where D4 is included your code will not run even if your cell D4 changed. Therefore you always need to work with Intersect.
Option Explicit
Sub Reset1(ByVal ws As Worksheet)
ws.Range("D20:E21,D8:E9,D6:E7").ClearContents
' alternative:
' Union(ws.Range("D20:E21"), ws.Range("D8:E9"), ws.Range("D6:E7")).ClearContents
End Sub
Sub Reset2(ByVal ws As Worksheet)
ws.Range("D20:E21,D8:E9").ClearContents
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
On Error Goto ENABLE_EVENTS ' in any case an error happens make sure events are enabeld again
If Not Intersect(Target, Me.Range("D4")) Is Nothing Then
Reset1 Me ' here we tell Reset1 to take `Me` as worksheet. Me refers to the worksheet `Target` is in.
End If
If Not Intersect(Target, Me.Range("D6")) Is Nothing Then
Reset2 Me
End If
ENABLE_EVENTS:
Application.EnableEvents = True
If Err.Number Then
Err.Raise Err.Number
End If
End Sub
I am using worksheet change event to trigger copy paste values. Worksheet change code is in the sheet2
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
Please note AB2 cell takes it's value from another sheet
Copy paste code is in a Module
Sub myTR1()
Sheets("BA1").Range("AR6:AS8").Value = Sheets("BA1").Range("AL17:AM19").Value
End Sub
When target range changes to "OK", my copy paste macro is not triggering. What am I doing wrong?
Using your eaxct code worked, although you didnt have end sub in your example?
EDIT:
Bear in mind the 'OK' is case sensitive so it will have to be in uppercase to fire, if you want it to fire either on lower or upper you can use the second code.
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
End Sub
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Or Target.Value = "ok" Then
Call myTR1
End If
End Sub
I'm running a Worksheet_Change sub that checks if the changed cell is in a specific column and then autofits that column.
Public Const startCol = 7 '(declared in a separate module)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = startCol Then
Target.Columns.AutoFit
End If
End Sub
My problem is, this macro is only run if something is put in the cell or the cell's value changes, not if the cell's value is cleared or the cell is deleted entirely.
Try this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = startCol Then
Columns(startCol).AutoFit
End If
End Sub
The Target is a range, you can then check if the range's value is equal to "" empy string ->
If Target.Value2 = "" Then MsgBox ("Emptied cell " & Target.Address)
Here is a kind-of solution, if you are wanting worksheet changes in column 7 AND deletion of contents in column 7 (via the delete key) then this kinda might be reasonable:
In ThisWorkbook section
Private Sub Workbook_Open()
Application.OnKey "{DELETE}", "ColFit"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "{DELETE}"
End Sub
In a module
Sub ColFit()
Select Case TypeName(Selection)
Case "Range"
Selection.ClearContents
Worksheets("Sheet1").Columns("G:G").AutoFit
Case "ChartArea"
ActiveChart.Parent.Delete
Case "PlotArea"
ActiveChart.Parent.Delete
End Select
End Sub
On the Worksheet
Private Sub Worksheet_selectionChange(ByVal Target As Range)
Worksheets("Sheet1").Columns(startCol).AutoFit
End Sub
save and reopen, now some more bases are covered ;)
also consider(in ThisWorkbook):
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Application.OnKey "{DELETE}", "ColFit"
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Application.OnKey "{DELETE}"
End Sub
which lets you work with multiple books without issues
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. :)
When I click the + beside a group of rows, how do I get it to hide a row outside of the grouped rows.
I tried this but it didn't work.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Rows(11).Hidden = True) Then
Rows(22).EntireRow.Hidden = False
Else
Rows(22).EntireRow.Hidden = True
End If
End Sub
Simply hiding or un-hiding a row will not trigger the Event, To use this Event, you must change a cell value.
EDIT#1
You can almost get what you want with the Worksheet_SelectionChange event.
Expand or collapse the Group of rows containing cell A11 and then click anywhere in the worksheet and row #22 will also expand/collapse. Put the following in a standard module:
Public Sub IsHiddenA11()
With Range("A11")
If .EntireRow.Hidden Then
Range("A22").EntireRow.Hidden = True
Else
Range("A22").EntireRow.Hidden = False
End If
End With
End Sub
and put this in the worksheet code area:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Call IsHiddenA11
Application.EnableEvents = True
End Sub