Excel - increase value at mouse click

Click For Summary
SUMMARY

The forum discussion centers on using Visual Basic for Applications (VBA) in Excel to increment the value in cell C8 by 1 each time cell C9 is double-clicked. A provided code snippet demonstrates the implementation of the Worksheet_BeforeDoubleClick event to achieve this functionality. The code effectively prevents the cell from entering edit mode by setting Cancel to True, allowing for repeated increments. Alternative methods, such as using SelectionChange or creating a button, are also mentioned for achieving similar results.

PREREQUISITES
  • Basic understanding of Excel and its interface
  • Familiarity with Visual Basic for Applications (VBA)
  • Knowledge of Excel event handling, specifically Worksheet_BeforeDoubleClick
  • Experience with cell referencing in Excel (e.g., Range object)
NEXT STEPS
  • Explore advanced VBA techniques for Excel automation
  • Learn about the SelectionChange event in VBA
  • Investigate creating buttons in Excel for user interaction
  • Study best practices for error handling in VBA scripts
USEFUL FOR

This discussion is beneficial for Excel users, VBA developers, and anyone looking to automate tasks within Excel spreadsheets through event-driven programming.

atferrari
Messages
8
Reaction score
0
I forgot long time ago the little I knew of VB applied to Excel.

Can anyone give a snippet of VB code to get the value in cell C8 to increasing by n every time I click the mouse on cell C9?

I recall that being possible but I am clueless now.

Gracias for any help.
 
Computer science news on Phys.org
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target = Range("C9") Then
        Range("C8").Value = Range("C8").Value + 1
        Cancel = True
    End If
End Sub

This works on double click. Setting Cancel to True prevents the cell from going into edit mode, so you can double click it again.

Doing it on click is a little harder, you can use SelectionChange for that, but that will only work if the cursor was not previously in C9. Otherwise, you can make a link or button in C9 and use that, but this is the most basic solution.
 
Neat! Gracias.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 18 ·
Replies
18
Views
6K