ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   add 1 when click (https://www.excelbanter.com/excel-programming/289369-add-1-when-click.html)

Dylan

add 1 when click
 
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and so
on.

Rob van Gelder[_4_]

add 1 when click
 
Dylan,

Right-click the Sheet Tab, View Code, Insert this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then Target.Value = Target.Value + 1
End Sub

If the Column is A then add 1


It will activate every time you select the cell. Unlike clicking a cell it
only triggers when you change the selection from a different cell.


"Dylan" wrote in message
...
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and so
on.




Rob van Gelder[_4_]

add 1 when click
 
Dylan,

Right-click the Sheet Tab, View Code, Insert this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then Target.Value = Target.Value + 1
End Sub

If the Column is A then it adds 1

It will activate every time you select the cell. Unlike clicking a cell it
only triggers when you change the selection from a different cell.

Rob


"Dylan" wrote in message
...
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and so
on.




Dylan

add 1 when click
 

-----Original Message-----
Dylan,

Right-click the Sheet Tab, View Code, Insert this:

Private Sub Worksheet_SelectionChange(ByVal Target As

Range)
If Target.Column = 1 Then Target.Value = Target.Value

+ 1
End Sub

If the Column is A then it adds 1

It will activate every time you select the cell. Unlike

clicking a cell it
only triggers when you change the selection from a

different cell.

Rob


"Dylan" wrote in message
...
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and

so
on.



.


hi Rob,

thanks for help. it works. what i see is that the whole
column will be affected. could you teach me how to amend
the script for only a particular Cell instead? also could
i do it such that the value change On_click instead of
clicking elsewhere then back on the cell. thanks very much.

Frank Kabel

add 1 when click
 
Hi Dylan
One way: Change the code to the following (now only A1 is counted):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("A1")) Is Nothing Then
Exit Sub
Else
Target.Value = Target.Value + 1
end if
End Sub

hi Rob,

thanks for help. it works. what i see is that the whole
column will be affected. could you teach me how to amend
the script for only a particular Cell instead? also could
i do it such that the value change On_click instead of
clicking elsewhere then back on the cell. thanks very much.




Bob Phillips[_6_]

add 1 when click
 
Hi Dylan,

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If not intersect (Target, Range("A1")) Is Nothing Then
Target.Value = Target.Value + 1
End If
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Dylan" wrote in message
...

-----Original Message-----
Dylan,

Right-click the Sheet Tab, View Code, Insert this:

Private Sub Worksheet_SelectionChange(ByVal Target As

Range)
If Target.Column = 1 Then Target.Value = Target.Value

+ 1
End Sub

If the Column is A then it adds 1

It will activate every time you select the cell. Unlike

clicking a cell it
only triggers when you change the selection from a

different cell.

Rob


"Dylan" wrote in message
...
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and

so
on.



.


hi Rob,

thanks for help. it works. what i see is that the whole
column will be affected. could you teach me how to amend
the script for only a particular Cell instead? also could
i do it such that the value change On_click instead of
clicking elsewhere then back on the cell. thanks very much.




Rob van Gelder[_4_]

add 1 when click
 
Dylan,

I don't think there is a trivial way to capture onclick for a worksheet.
Because SelectionChange fires even when you use the cursor keys to enter
that cell, I suggest another approach:

Create a button and assign this macro to it, which in this example
increments selection if it falls within the range A1:B20
If selection is outside of bounds, then it beeps as an error.

Sub Increment_Click()
Dim rng As Range, rngSel As Range

Set rngSel = Intersect(Selection, Range("A1:B20"))
If Not rngSel Is Nothing Then
For Each rng In rngSel
rng.Value = rng.Value + 1
Next
Else
Beep
End If
End Sub


Rob


"Dylan" wrote in message
...

-----Original Message-----
Dylan,

Right-click the Sheet Tab, View Code, Insert this:

Private Sub Worksheet_SelectionChange(ByVal Target As

Range)
If Target.Column = 1 Then Target.Value = Target.Value

+ 1
End Sub

If the Column is A then it adds 1

It will activate every time you select the cell. Unlike

clicking a cell it
only triggers when you change the selection from a

different cell.

Rob


"Dylan" wrote in message
...
Hi,
does anyone know how to program a cell when every single
click on that cell with mouse button, the value of the
cell will add 1. for example: Cell A1 had a value of 7,
when i click on cell A1, the value will go up to 8 and

so
on.



.


hi Rob,

thanks for help. it works. what i see is that the whole
column will be affected. could you teach me how to amend
the script for only a particular Cell instead? also could
i do it such that the value change On_click instead of
clicking elsewhere then back on the cell. thanks very much.





All times are GMT +1. The time now is 01:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com