How can I capture the event in Excel when a user clicks on a cell. I want to be able to use this event to trigger some code to count how many times the user clicks on several different cells in a column.
Check out the Worksheet_SelectionChange event. In that event you could use Intersect() with named ranges to figure out if a specific range were clicked.
Here's some code that might help you get started.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("SomeNamedRange")) Is Nothing Then
'Your counting code
End If
End Sub
Use the Worksheet.SelectionChange event to trap this.
Worksheet SelectionChange event would do it. Note that this fires every time user clicks a new cell.
Related
I have a database which auto refreshes and updates the table from an external source every 15 minutes. I tried the following code which updates the PivotTable every time the source data is edit/added/deleted:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Worksheets("PIVOT TABLE WORKSHEET").PivotTables("PIVOT TABLE NAME").RefreshTable
End Sub
When i manually edit the source data, the PivotTables refresh accordingly. However, when the source data is updated automatically the PivotTables remain unchanged. Is there a way to make the Pivot tables refresh together with the database without the need of a user input?
You'll have better results if you use the Worksheet_Change event instead of the Worksheet_SelectionChange event, so that the procedure runs when data changes, not when you select a cell with the mouse or keyboard.
Worksheet_Change vs. Worksheet_SelectionChange events
Worksheet_SelectionChange fires when the selection changes on a worksheet.
For example, when the user clicks on a cell, or pushes an arrow key.
Worksheet_Change fires when cells on the worksheet are changed, either by the user or by an external link.
Note: Worksheet_Change does **not** occur when cells change during a re-calculation; use theCalculate` event to trap a sheet re-calculation.)
Depending on how the data is laid out in your worksheet(s), you may want to limit the execution of this procedure by checking which cell(s) were changed, which is easiest overall by comparing the event procedure's Target parameter to a specific cell or cell-range, using the Intersect function.
Caution: Beware of infinity!
When using code that changes cells within the area being "watched" by the Worksheet_Change event procedure, you risk entering into an infinite loop, since the change fires the event again, which changes cells again, etc.
(Click image to enlarge.)
There are several ways this could be avoided. This most common is to temporarily disable events with the Application.EnableEvents property while the Change event does what it needs to do. (Don't forget to re-enable events at the end of the procedure... see example below.)
Example:
Here's an untested example using all these points:
Private Sub Worksheet_Change(ByVal Target As Range)
Const cellsToWatch = "A1:D4"
With Worksheets("PIVOT TABLE WORKSHEET")
'exit the procedure if at least part of the changed cells were not within `A1:D4`
If Application.Intersect(.Range(cellsToWatch), Target) Is Nothing Then
'the change wasn't within `cellsToWatch`
Exit Sub
End If
Application.EnableEvents = False 'disable execution of this or other events
'----------Run your code here:--------------
.Calculate
.PivotTables("PIVOT TABLE NAME").RefreshTable
'-------------------------------------------
Application.EnableEvents = True 're-enable events
End With
End Sub
More Information:
MSDN : Worksheet_Change event (Excel)
MSDN : Worksheet_SelectionChange event (Excel)
MSDN : Application.Intersect method (Excel/VBA)
Office Support : Intercept function (Excel/Worksheet)
Stack Overflow : Stop Excel from firing Worksheet_Change before _BeforeSave?
MSDN : Application.EnableEvents property (Excel)
I'd like to know on how to code a userform (VBA Excel) with automatically updating itself when the cell values has changed.
I have produced a button that will show the userform with labels and text boxes. But whenever i click it yes it shows up but i need to click the userform in order for me to see the values.
Need help.
Thank you in advance,
Tramyer
The Worksheet.Change event is fired everytime you change the contents of a cell on a given worksheet.
You can create an event handler in the worksheet module (usually labeled Sheet1, Sheet2 etc. in the VBA editor) that is called every time the event is fired:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
This example just outputs the address of the cell that was changed, but you can adapt this to update your userform with the changed values instead.
I have an Excel ListBox (not ActiveX as these are causing display issues) with an Excel (dialog menu-driven) macro that outputs its value to a named range cell on a different sheet to the Listbox. In that sheet's code I have the below
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print "ping"
End Sub
The macro event does not trigger when I click on the Listbox (therefore updating the named range cell value). I have verified that the macro executes when I directly update that sheet.
I assume the form control macro is circumnavigating the sheet event trigger. Am I right? Does anybody know an efficient workaround for this? I am stumped.
Thanks Kindly
You can assign a macro to the ListBox as ListBox1_Change event (right click › assign macro) which will be executed on change of the ListBox value instead of Worksheet_Change then.
Probably that is why the Worksheet_Change is not triggered anymore when using the ListBox to change the cell value.
I am using Excel Comments to store some information that I want to allow the user to edit using a form. I want to pre-populate the form with the text contained in the Comment when the user selects the Comment. I can do this when the user selects the cell with the red tab (when the Comments are hidden) using the SheetSelectionChange event. But is there a way to do this when the Comments are shown and the user clicks inside the Comment box?
I can't find any events associated with Comments in the help. Are there any Comment Events exposed to VBA? Or can I do this with some Cell or Sheet event? I tried putting a MsgBox inside the SheetSelectionChange Event to show the Target.ActiveCell, but when I select a Comment, I'm not getting a response, so it seems like when selecting a Comment, it is not associated with a sheet.
Thanks in advance for any tips.
I do not believe there are any events for comments.
However you could use the SelectionChange Event on the sheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (Target.Comment Is Nothing) Then MsgBox Target.Comment.Text
End Sub
To use within your Form:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (Target.Comment Is Nothing) Then
frmYourForm.Show
frmYourForm.txtComments = Target.Comment.Text
End If
End Sub
I am trying to monitor a sheet which has several text boxes on it.
The idea behind it is the user enters text into these boxes and then you can click the submit button to send it all to a sql database.
Now I want to have it so if the user has made changes, and they go to leave the sheet a macro is triggered to tell them that they haven't saved it.
I've already got this triggering on the deactivate worksheet event, but I was wondering if I can monitor all of the textboxes on the sheet (oleobjects) under one change event
I am already assuming this isn't possible with just one but was hoping.
Thanks in advance
Tom
One way to do this would be to write a separate subroutine that is called within the Change event of all the Textboxes. Keep in mind though that this will be raised every time the Textboxes change at all- every keystroke.
In each TextBox_Change Event:
Private Sub TextBox1_Change()
TextChanged TextBox1
End Sub
New subroutine:
Sub TextChanged(fromTextBox As TextBox)
'validation code goes here
End Sub