Macro code to circle items when checkbox is checked.

aking154

New Member
Joined
Aug 3, 2009
Messages
3
I have several worksheets with pricing and checkboxes that when checked total the costs up automatically. I have a code that adds the checkboxes for me automatcally and also links them to the proper cell for the true and false at the same time. I've added conditional formatting to make the price cell turn yellow and bold the type when the checkbox is checked, but when printed in black and white, this is not obvious enough that this is the choice that has been selected and the checkboxes are so small that you can hardly tell when they are checked. My question is can I add code to what I already have that will also add a circle around the price when the checkbox is checked? Here is the code I have:

Option Explicit<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Sub insertCheckboxes()<o:p></o:p>
<o:p></o:p>
Dim myBox As CheckBox<o:p></o:p>
Dim myCell As Range<o:p></o:p>
<o:p></o:p>
Dim cellRange As String<o:p></o:p>
Dim cboxLabel As String<o:p></o:p>
Dim linkedColumn As String<o:p></o:p>
<o:p></o:p>
cellRange = InputBox(Prompt:="<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:place><st1:PlaceName>Cell</st1:PlaceName> <st1:PlaceType>Range</st1:PlaceType></st1:place>", _<o:p></o:p>
Title:="<st1:place><st1:PlaceName>Cell</st1:PlaceName> <st1:PlaceType>Range</st1:PlaceType></st1:place>")<o:p></o:p>
<o:p></o:p>
linkedColumn = InputBox(Prompt:="Linked Column", _<o:p></o:p>
Title:="Linked Column")<o:p></o:p>
<o:p></o:p>
cboxLabel = InputBox(Prompt:="Checkbox Label", _<o:p></o:p>
Title:="Checkbox Label")<o:p></o:p>
<o:p></o:p>
With ActiveSheet<o:p></o:p>
For Each myCell In .Range(cellRange).Cells<o:p></o:p>
With myCell<o:p></o:p>
Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _<o:p></o:p>
Width:=.Width, Left:=.Left, Height:=.Height)<o:p></o:p>
<o:p></o:p>
With myBox<o:p></o:p>
.LinkedCell = linkedColumn & myCell.Row<o:p></o:p>
.Locked = False 'This unlocks the checkbox also<o:p></o:p>
.Caption = cboxLabel<o:p></o:p>
.Name = "checkbox_" & myCell.Address(0, 0)<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
.NumberFormat = ";;;"<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
Next myCell<o:p></o:p>
End With<o:p></o:p>
End Sub<o:p></o:p>
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi and welcome to the board.

Fisrt let me say that your code needs some fair amount of error trapping . I recommend you reedit your code to make it more error proof.

A while ago our board member Tom Urtis posted here a cool way to circle text without the need for adding shapes or VBA. It basically uses Data Validation.

I have built on this method to automate circling the text in your checkbox linked cells if they display TRUE every time the sheet is printed.

In a standard module : ( This is an amendment of your own code )

Code:
Option Explicit
 
Public olinkedColumn As Range
Public oActiveSheet As Worksheet
Public ocellRange As Range
 
Sub insertCheckboxes()
 
    Dim myBox As CheckBox
    Dim myCell As Range
    
    Dim cboxLabel As String
    
    Set ocellRange = _
    Application.InputBox(Prompt:="CellRange", _
    Title:="CellRange", Type:=8)
    
    Set olinkedColumn = _
    Application.InputBox(Prompt:="Linked Column", _
    Title:="Linked Column", Type:=8)
    
    Set oActiveSheet = ActiveSheet
    
    cboxLabel = InputBox(Prompt:="Checkbox Label", _
    Title:="Checkbox Label")
    
    With ActiveSheet
        For Each myCell In ocellRange.Cells
            With myCell
            Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _
            Width:=.Width, Left:=.Left, Height:=.Height)
            With myBox
                .LinkedCell = _
                Cells(myCell.Row, olinkedColumn.Column).Address
                .Locked = False 'This unlocks the checkbox also
                .Caption = cboxLabel
                .Name = "checkbox_" & myCell.Address(0, 0)
                With Range(.LinkedCell)
                    On Error Resume Next
                    .Validation.Delete
                    On Error GoTo 0
                    .Validation.Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="=" & _
                    .Address(False, False) & "<>TRUE"
                    .Validation.ShowError = False
                End With
            End With
            .NumberFormat = ";;;"
            End With
        Next myCell
    End With
    
End Sub

And add this to the WorkBook module :

Code:
Option Explicit
 
Private Sub Workbook_BeforePrint(Cancel As Boolean)
 
Dim lRowsCount As Long
 
On Error Resume Next
    If Not oActiveSheet Is Nothing Then
    If ActiveSheet Is oActiveSheet Then
        With ActiveSheet
            .CircleInvalid
            lRowsCount = ocellRange.Rows.Count
            olinkedColumn.Range _
            (ocellRange.Cells(1, 1), _
            ocellRange.Cells(lRowsCount, 1)).Select
            Selection.CopyPicture _
            Appearance:=xlScreen, Format:=xlBitmap
            .Paste
            .ClearCircles
            Application.OnTime Now + _
            TimeSerial(0, 0, 1), Me.CodeName & ".RemoveCircles"
        End With
    End If
    End If

End Sub

Private Sub RemoveCircles()
 
    Selection.Delete
    ocellRange.Cells(1).Select
 
End Sub

I hope this helps.

Regards.
 
Upvote 0
Thank you so much, Jafar. The code I originally had was something that I found - I am a novice when it comes to writing code and could never have done that - errors or not!

I'm running into an issue with the code you provided. I've added them to my modules and the workbook like you said, however, the checkboxes are added fine, but there are no circles coming up when I click on the checkboxes. Also - when I go to the workbook and run the macro, I'm getting an error notice - "Compile Error - Variable not defined" and it's highlighting "ocellrange" - do I need to add in the cell range that I used for the checkboxes?

Thank you again!
 
Upvote 0
When you click on the checkboxes, no circles should come up. The circles will appear only when you print the worksheet or print- preview the worksheet.
That is what you requested on your original post.

here is a workbook demo.

Note the code should be run once .

Regards.
 
Upvote 0
HI Jaafar,

I'm sorry that I'm not as adept at this as I should be, but I cannot get this to work and I cannot open the demo - I just get a free offer that won't allow me to open the page at all unless I enter my address and information.

I keep getting the error messages and I'm not sure what you mean by the fact that I can only run the macro once, as I have several columns to add checkboxes to. Can you elaborate further, please?

Thank you!
 
Upvote 0
Hi,

I am sorry but i am really busy at the moment.

See if you can download the workbook demo from this link.

Regards.
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,928
Members
449,274
Latest member
mrcsbenson

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