Activate code by clicking in cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the best way to code so that when clicking on one of a few dozen
cells that cell is toggled between blank and an check mark symbol?
I found a post that explains using TargetAddress in the sheet code but I
would need a separate routine for each cell that I want to react this way.
Seems like there might be a better way.
I want to put the check box in the cell rather than using the check box
control becuase I have set up extensive conditional formatting of the cells
based on whether there is a check mark or not.

Thanks in advance for any help.
ed
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrHandler
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("F14:H26")) Is Nothing Then
Application.EnableEvents = False
If Len(Trim(Target)) = 0 Then
Target.Value = "X"
Else
Target.ClearContents
End If
End if
ErrHandler:
Application.EnableEvents = True
End Sub
 
This worksheet code will work on an abritrary collection of cells, but with
double-click:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
Set r = Union(Range("A1"), Range("B2"), Range("C3"))
If Intersect(r, Target) Is Nothing Then
Cancel = False
Exit Sub
End If
If IsEmpty(Target) Then
Target.Value = "a"
Cancel = True
Else
Target.Value = ""
Cancel = True
End If
End Sub

and format the cells in question as font-webdings
 
This worked well, for a while.
First I copied your code directly. Works fine.
Then I tried using a named range, since I wanted to limit the feature to
non-adjacent cells. That also worked fine.
Then I wanted to use a check instead of an X, so I changed the line
Target.Value = "X" to
Target.Value = Cells (1,15) and put a checkbox in O1. This also worked fine.

So now I'm ready to try it in my project.
I tried creating a named range with all the needed cells, but there were too
many, so I played around a bit with adding named ranges into one named range,
but got stuck, so I decided to try your code with my modifications on the
named range that is a subset of my total desired range.
This would not work.
To trouble shoot I tried several ideas.
1. The Worksheet code already had commandbutton code. I hought that might
somehow interfere, so I commented that out. No difference.
2. The cells I am using had validation turned on so I could have a dropdown
for the checkbox. I turned that off. Still would not work.
3. Tried using a more limited direct reference range instead of the named
range. No good. Also tried inserting Gary's Stuident code with the Union
(range, range,etc.) somewhere along here.
4. Went back to a direct value of "X" for the Target.Value. Nope
5. Went back to the practice sheet. Not sure what changes I made, if any,
but now that code will not work.
6. In the practice sheet, started over with a fresh paste of your code. no
7. Tried in a fresh tab. Still nothing.
8. Tried a fresh workbook. Cut and paste the code. Still will not work.
Now I'm stumped.
HELP (plese)
 

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

Back
Top