Checkboxes disappear after macro is run

Jonasmj

New Member
Joined
Sep 23, 2002
Messages
38
Hi

I have a macro that loops through a bunch of checkboxes to see if they are checked or not. If they are not checked the row it sits on plus the row below is hidden. Also the unchecked checkbox is also hidden. After this I call on print preview.

Code:
Sub print_checked()

    Dim obj As OLEObject
    Dim i As Integer
    
    Application.ScreenUpdating = False
    
    For Each obj In ActiveSheet.OLEObjects
        'first check to see if the checkbox really is a checkbox
        If TypeOf obj.Object Is MSForms.CheckBox Then
             'then if the checkbox is not checked i hide rows and the checkbox itself
                If obj.Object = 0 Then
                ActiveSheet.Rows(obj.TopLeftCell.row).EntireRow.Hidden = True
                ActiveSheet.Rows(obj.TopLeftCell.row + 1).EntireRow.Hidden = True
                obj.Visible = False
             End If
         End If
    
    Next obj
    
    Application.ScreenUpdating = True
       
    ActiveWindow.SelectedSheets.PrintPreview
    
End Sub

When printing is done I wish to restore the sheet to its orginal look with all rows and checkboxes visible again. Therefore I have this code:
Code:
Sub restore_from_print()

    Dim obj As OLEObject
    Dim i As Integer

    
    Application.ScreenUpdating = False

    For Each obj In ActiveSheet.OLEObjects
        If TypeOf obj.Object Is MSForms.CheckBox Then
                If obj.Object = 0 Then
                ActiveSheet.Rows(obj.TopLeftCell.row).EntireRow.Hidden = False
                ActiveSheet.Rows(obj.TopLeftCell.row + 1).EntireRow.Hidden = False
                obj.Visible = True
             End If
        End If
    
    Next obj
     
    Application.ScreenUpdating = True
     
End Sub

BUT neither the rows or the checkboxes get visible, at least not all of them. Sometimes it seems a couple of rows are shown. If I run the code without calling on print preview the restore-code seems to work fine. ?!

What am I doing wrong here?

Regards Jonas
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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