WorkSheet_Change event

C

Corey

I am trying to create a simple code to place/remove a 'X' in a various cells across a sheet IF the cell is clicked.
There are about 20 individual cells:
A12,A14,A16,A18.A20,A22,A24,E12,E14,E16,E18,E20,E22,E24,
K12,K14,K16,K18,K20,K22,K24,Q,12,Q14,Q16, Q20,Q22,Q24.

If the user selects one of the cells, then a 'X' appears.
If the cell already had a 'X' in it, then the cell clears.

How can i accomplish this ?
 
J

JLGWhiz

It might be a little primitive, you could set up a If...Then...ElseIf
algorithm for those cells using the Worksheet SelectionChange event.
 
D

Don Guillett

Modify to suit
Sub replacex()
Range("h9,h11,h13,h15").Replace "x", ""
End Sub


--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
I am trying to create a simple code to place/remove a 'X' in a various cells across a sheet IF the cell is clicked.
There are about 20 individual cells:
A12,A14,A16,A18.A20,A22,A24,E12,E14,E16,E18,E20,E22,E24,
K12,K14,K16,K18,K20,K22,K24,Q,12,Q14,Q16, Q20,Q22,Q24.

If the user selects one of the cells, then a 'X' appears.
If the cell already had a 'X' in it, then the cell clears.

How can i accomplish this ?
 
D

Don Guillett

Modify to suit

Sub replacex()
Range("h9,h11,h13,h15").Replace "x", ""
End Sub


--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
I am trying to create a simple code to place/remove a 'X' in a various cells
across a sheet IF the cell is clicked.
There are about 20 individual cells:
A12,A14,A16,A18.A20,A22,A24,E12,E14,E16,E18,E20,E22,E24,
K12,K14,K16,K18,K20,K22,K24,Q,12,Q14,Q16, Q20,Q22,Q24.

If the user selects one of the cells, then a 'X' appears.
If the cell already had a 'X' in it, then the cell clears.

How can i accomplish this ?
 
J

JLGWhiz

You could probably do it with just a If..Then..Else statement using the Or
operator for each cell, but it would still be a long statement.
 
D

Don Guillett

A re-read did say IF clicked. How about IF double clicked.
Right click sheet tab>view code>copy/Paste this>finish adding your ranges.
Then, when appropriate cells are double clicked the macro will fire.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
If Intersect(Target, Range("a12,a14,a16,a18")) Is Nothing Then Exit Sub
If UCase(Target) = "X" Then Target.Replace "X", ""
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(e-mail address removed)
I am trying to create a simple code to place/remove a 'X' in a various cells
across a sheet IF the cell is clicked.
There are about 20 individual cells:
A12,A14,A16,A18.A20,A22,A24,E12,E14,E16,E18,E20,E22,E24,
K12,K14,K16,K18,K20,K22,K24,Q,12,Q14,Q16, Q20,Q22,Q24.

If the user selects one of the cells, then a 'X' appears.
If the cell already had a 'X' in it, then the cell clears.

How can i accomplish this ?
 
R

rumkus

With selection change event, something like below.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

If Intersect(Target,
Me.Range("A12,A14,A16,A18,A20,A22,A24,E12,E14,E16,E18,E20,E22,E24,K12,K14,K16,K18,K20,K22,K24,Q12,Q14,Q16,Q20,Q22,Q24"))
Is Nothing Then Exit Sub

If Len(Target) = 0 Then
Target.Value = "x"
Exit Sub
Else
Target.Value = ""
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top