Simulating Checkboxes In The Cell Itself

gaming_mouse

Board Regular
Joined
Apr 6, 2005
Messages
139
My search turned up an old thread describing exactly what I want, but the solution was emailed to the poster rather than posted:

http://www.mrexcel.com/board2/viewtopic.php?t=176712&highlight=checkbox+create

Basically, I want to be able to click on a cell and have it become "checked" and "unchecked" with an "X" or some other mark, but I don't want to use a form checkbox, as those add file size and seem cludgy. Can anyone tell me how to do this?

Thanks,
gm
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then
    If Target.Value = "X" Then
        Target.Value = ""
    Else
        Target.Value = "X"
    End If
End If
End Sub

The Column = 1 is for what column will be looking for the X. Click a cell in the column you specify after pasting this code to the sheet you want it and see what happens.
 
Upvote 0
anthonya2369 said:
The Column = 1 is for what column will be looking for the X. Click a cell in the column you specify after pasting this code to the sheet you want it and see what happens.

thanks, anthony.

one question. when you have a column number hardcoded into VBA code like that, will it change if you insert another column, or do you have to manually adjust your code in that case?

Thanks,
gm
 
Upvote 0
Hello

Another option would be to insert this into the Thisworkbook section of your project and edit if necessary.

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Target.Address = Sheets("Sheet1").Range("A1").Address Then
    Cancel = True
    If Target.Value = "X" Then
        Target.Value = ""
    Else
        Target.Value = "X"
    End If
End If
End Sub

This way, the X will only change if the specified cell is double clicked, furthermore, the macro will only run when the sheet is double clicked therefore not running the macro everytime a selection change is made.

Hope this helps, and it is what you're looking for.
 
Upvote 0
Hi again

Here is a better option:

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Set intsect = Application.Intersect(Target, Range("A1:A100"))
If intsect Is Nothing Then
    Exit Sub
Else
    Cancel = True
    If Target.Value = "X" Then
        Target.Value = ""
    Else
        Target.Value = "X"
    End If
End If
End Sub

Effectively checks to see if the double clicked cell is in the range A1:A100, if so, runs the macro.

Hope this helps
 
Upvote 0
but I don't want to use a form checkbox, as those add file size and seem cludgy.
Try: View - Toolbar and choose Control Toolbox and insert the Check Box. Can't tell you, though, if they're less in size. I wouldn't really believe it.

RAM

Edit: My bad. I misunderstood the question and withdraw my comment.
 
Upvote 0
Fausto said:
Hi again

Here is a better option:

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Set intsect = Application.Intersect(Target, Range("A1:A100"))
If intsect Is Nothing Then
    Exit Sub
Else
    Cancel = True
    If Target.Value = "X" Then
        Target.Value = ""
    Else
        Target.Value = "X"
    End If
End If
End Sub

Effectively checks to see if the double clicked cell is in the range A1:A100, if so, runs the macro.

Hope this helps

Fausto,

Thanks for that solution. Can you explain what is happening with "Cancel"?

Thanks,
gm
 
Upvote 0
RAM said:
but I don't want to use a form checkbox, as those add file size and seem cludgy.
Try: View - Toolbar and choose Control Toolbox and insert the Check Box. Can't tell you, though, if they're less in size. I wouldn't really believe it.

RAM

No, I want a true in-cell checkbox. thanks for the thought, tho.
 
Upvote 0
Edit: My bad. I misunderstood the question and withdraw my comment.
 
Upvote 0
Hello gaming_mouse

Effectively the Cancel = True is telling Excel not to open the cell for editing. If you comment out that line or delete it all together you will see that if you double click the cells in the range A1:A100, the cell will still be open for editing after double clicking. With my solution, the X is placed in the cell without having to press enter everytime you double click.

Reading this reply, I can see that it is not very clear. I suggest you try it for yourself to see what I mean.

Hope this answers your question.
 
Upvote 0

Forum statistics

Threads
1,203,454
Messages
6,055,539
Members
444,794
Latest member
HSAL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top