Checkbox controls disappear on sort

Hi all....
Excel 2010
Checkbox control (ActiveX)

My Sheet has 6 checkbox controls per record. Works great until I do a sort via vba
a couple of the checkboxes disappear!!!
What am I missing....???
LVL 14
BigBadWolf_000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
TommySzalapskiCommented:
Just right-click on all the controls and select Format control. On properties, uncheck 'move and size with cells'
0
 
BigBadWolf_000Author Commented:
Thanks.....I thought I would need that checked...however makes sense specialy since the checkbox is overlayed on the cell....I will try it and get back.
0
 
BigBadWolf_000Author Commented:
Hi TommySzalapski:
Under Format Control > Properties > Following are the radio button options (have to choose one)
Move and size with cells
Move but don't size with cells
Don't move or size with cells

Default was #2 - but some checkboxes disappear with every sort

Tried #1 and #3 - some checkboxes disappear with every sort and also some vanish when you click on the checkbox

Any ideas?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
TommySzalapskiCommented:
Vanish when you click the checkbox? Do you have macros running? Could you post the file? (Remove any private info of course).
0
 
BigBadWolf_000Author Commented:
Yes macros are running on checking the box and unchecking the box (sheet is also protected)

Below is the sort macro that causes the issue

If that does not help...I will scrub and post file...


Sub Macro3()
'
' Macro3 Sort Acsending
'
' Keyboard Shortcut: Ctrl+a
'
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowSorting:=True, Password:="MyPwd", userinterfaceonly:=True

    ActiveSheet.Sort.SortFields.Clear
    sort1 = InputBox("Enter Letter of Column to Sort Ascending")
    
    ActiveSheet.Sort.SortFields.Add Key:=Range(sort1 & "3:" & sort1 & "27") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    With ActiveSheet.Sort
        .SetRange Range("A1:AP27")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Protect ("MyPwd")
    Range("A1").Select
End Sub

Sub Macro4()
'
' Macro4 Sort Descending
'
' Keyboard Shortcut: Ctrl+d
'
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowSorting:=True, Password:="MyPwd", userinterfaceonly:=True

    ActiveSheet.Sort.SortFields.Clear
    sort1 = InputBox("Enter Letter of Column to Sort Descending")
    
    ActiveSheet.Sort.SortFields.Add Key:=Range(sort1 & "3:" & sort1 & "27") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

    With ActiveSheet.Sort
        .SetRange Range("A1:AP27")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Protect ("MyPwd")
    Range("A1").Select
End Sub

Open in new window

0
 
BigBadWolf_000Author Commented:
Either the acending or desinding macro causes the issue
0
 
TommySzalapskiCommented:
My guess is they are not 'vanishing' they are just moving on top/under another checkbox. Setting 'do not move or size with cells' should keep them from moving (you'd need to set it on every one, even the ones that are hiding under other ones). It is possible to do this to all of them it a macro.
However, my guess is you actually want them to stay with their correct rows when they are sorted so you probably want them to 'move but don't size with cells'

Now here's the funny issue, if the cell is too small or the checkbox too large that the checkbox isn't entirely within the cell, it won't move at all. What's really odd is that 'undo' does not fix it. They stay moved, but the 'vanished' ones are still there.

So if some of your checkboxes are completely in cells and some aren't, you will see the behavior you describe.

I posted a file showing failing and working examples in two sheets
check.xls
0
 
TommySzalapskiCommented:
If that's not the issue, I'll need to see a sample file showing your error.
0
 
BigBadWolf_000Author Commented:
Thanks,
Unfortunately that was not the issue.
Attached file

Protection pwd is = MyPwd

Happens only on a sort with ctrl+d or ctrl+a

Please test with pwd enabled.
See Help sheet for macro hotkeys CI-v1.0.xlsm
0
 
BigBadWolf_000Author Commented:
I noticed an issue....the checkboxes cannot be moved on sort cause they referance an adjacent cell in the onclick macro
0
 
BigBadWolf_000Author Commented:
if "Don't move or size with cells" then I loose the checkboxes on the Ctrh+H hide macro
Also it still moves the checkboxes on the sort macro

I guess may have to consider another sort methadology if even possible :O
0
 
TommySzalapskiCommented:
I'm not seeing any disappear, but here is how I got it working.
When the sheet is protected, the controls don't move right, even if 'allow sort' is set. So at the top of each of the two sort macros just put
ActiveSheet.Unprotect "MyPwd"

If you're worried about the macro crashing before protection gets turned back on just set On Error GoTo crash and add a label called crash right before the protection turns back on.
You could even display the error too.
Sub Macro3()
'
' Macro3 Sort Acsending
'
' Keyboard Shortcut: Ctrl+a
'
    On Error Goto crash
    ActiveSheet.Unprotect "MyPwd"

    ActiveSheet.Sort.SortFields.Clear
    sort1 = InputBox("Enter Letter of Column to Sort Ascending")
    'sort2 = InputBox("enter second sort column")
    
    ActiveSheet.Sort.SortFields.Add Key:=Range(sort1 & "3:" & sort1 & "27") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    'ActiveSheet.Sort.SortFields.Add Key:=Range(sort2 & "3:" & sort2 & "27") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A1:AP27")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
crash:
    ActiveSheet.Protect ("MyPwd")
    If Err.Number <> 0
        MsgBox Err.Description
    End If
    Range("A1").Select
End Sub

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
BigBadWolf_000Author Commented:
Hi TommySzalapski:
using just ActiveSheet.Unprotect "MyPwd" worked, however I do no want the checkboxes to move on the sort ...so when I change the properties to "Don't move or size with cells"
the checkboxes disappear or you have to double click on the checkbox to get the check mark(click on each checkbox with dates to see the issue)

Attached updated file. Alternative - keep -Move but don't size with cells
but remove the columns that contain the checkboxes from sort---if even possible

Any thoughts? CI-v2.0.xlsm
0
 
TommySzalapskiCommented:
But don't the checkboxes also relate to the cells in the row? So shouldn't they follow them? You can reset the linked cell property in the code when you sort so the boxes will move but they will still update the cells beneath them.
0
 
TommySzalapskiCommented:
I also recommend you clean up the code some. Like change Macro1 and Macro2 to something like the following (Using ActiveSheet usually isn't good because if the user clicks on a different sheet while the code is running, it does bad things)
Sub Macro1()
'
' Macro1 Hide
'
' Keyboard Shortcut: Ctrl+h
'
    Dim shp As Shape
    
    Sheet1.Unprotect ("MyPwd")
    Sheet1.Range("G:G,I:N,T:T,W:AD,AI:AI,AK:AP").EntireColumn.Hidden = True
    
    For Each shp In Sheet1.Shapes
      If Left(shp.Name, 8) = "CheckBox" And Right(shp.Name) <> "a" And Right(shp.Name) <> "f" Then
        shp.Visible = False
      End If
    Next

    Sheet1.Protect ("MyPwd")
End Sub
Sub Macro2()
'
' Macro2 Unhide
'
' Keyboard Shortcut: Ctrl+u
'
    Dim shp As Shape
    
    Sheet1.Unprotect ("MyPwd")
    Sheet1.EntireColumn.Hidden = False
    
    'Sheet1.Range("A1:A2").Select Do you need this?
    For Each shp In Sheet1.Shapes
      If Left(shp.Name, 8) = "CheckBox" And Right(shp.Name) <> "a" And Right(shp.Name) <> "f" Then
        shp.Visible = False
      End If
    Next
    
    Sheet1.Protect ("MyPwd")
End Sub

Open in new window

0
 
TommySzalapskiCommented:
Those need to be Right(shp.Name, 1)
0
 
TommySzalapskiCommented:
Also, the checkboxes know where they are. Instead of using Range("U3") for the box in V3 use
Checkbox1a.TopLeftCell.Offset(0,-1) to select the cell one to the left of where the checkbox is.
0
 
TommySzalapskiCommented:
So you could update all the Click events with code like the following.
By the way, you can add the click events to all the checkboxes at the same time with the same macro. See this solution to this question
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_26626128.html

(Of course change all the references to labels to checkboxes)
Private Sub chk_Click()
  Dim r As Range
  Sheet1.Unprotect ("MyPwd")
  Set r = CheckBox1a.TopLeftCell.Offset(0, -1)
 
 If chk.Value = True Then
    r.ClearFormats
    r.NumberFormat = "M/D/YYYY"
    With r.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With r.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With r.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    Sheet1.Protect ("MyPwd")
 Else
    r.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=TODAY()", Formula2:="=TODAY()+15"
    r.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With r.FormatConditions(1).Font
        .Color = -16777024
        .TintAndShade = 0
    End With
    With r.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.799981688894314
    End With
    With r.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With r.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With r.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    r.FormatConditions(1).StopIfTrue = False
    Sheet1.Protect ("MyPwd")
 End If
End Sub

Open in new window

0
 
BigBadWolf_000Author Commented:
Thanks - I will try it out tomorrow
0
 
BigBadWolf_000Author Commented:
Thnak you for all your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.