Consulting

Results 1 to 6 of 6

Thread: VBA CheckBox issues

  1. #1
    VBAX Regular
    Joined
    Dec 2014
    Posts
    10
    Location

    VBA CheckBox issues

    I have a macro which generates a new spreadsheet and within that spreadsheet it creates several check boxes and a button. The button is assigned a macro which should hide or show the check boxes if they are pertinent or not. When I attempt to run the code through the button I get the following error: Run Time error 424 Object Required. Here's some of the code:


    The Check boxes are created like this:

    ActiveSheet.CheckBoxes.Add(Left:=Range("E6").Left, Top:=Range("E6").Top, Width:=Range("E6").Width, Height:=Range("E6").Height).Select
            With Selection
                .Name = CheckBox1
                .Caption = "All personal transactions have been properly Flagged"
            End With

    Then a separate sub contains the code for hiding or showing them attached to the button like this:

      If Sheets("Review Report").Range("B6").Value > 0 Then
            Sheets("Review Report").Range("B6").Interior.ColorIndex = 27
            CheckBox1.Visible = True
        Else:
            Sheets("Review Report").Range("B6").Interior.ColorIndex = 0
            CheckBox1.Visible = False
    The error comes from this line:
    CheckBox1.Visible = True
    I wonder if the problem comes from the fact that the checkboxes are generated in one macro and then the second macro cannot find those?
    Last edited by SamT; 12-04-2014 at 02:53 PM. Reason: Insert Code Tages with "#" button

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    I think the .visible property is an only read one, ie. you can check the status but that's the only thing you can do.
    To alter the visibility of an object, try using .Hidden = False to see if you can see the checkbox.
    Think worksheets work the same way.

    Charlize

    ps. or maybe add the sheetreference to the checkbox ?

  3. #3
    VBAX Regular
    Joined
    Dec 2014
    Posts
    10
    Location
    I finally figured it out this worked:
    ActiveSheet.CheckBoxes(1).Visible = True

    I'm not sure why that works but just putting: SheetName.CheckBox1.Visible = True
    doesn't work. Thanks for the help though!

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Forget what I said . You have to loop through the boxes you made on your sheet.
    Sub test()
    Dim theboxes As Long
    ActiveSheet.CheckBoxes.Add(Left:=Range("E6").Left, Top:=Range("E6").Top, Width:=Range("E6").Width, Height:=Range("E6").Height).Select
    With Selection
        .Name = CheckBox1
        .Caption = "All personal transactions have been properly Flagged"
    End With
    If ActiveSheet.CheckBoxes.Count > 0 Then
        For theboxes = 1 To ActiveSheet.CheckBoxes.Count
            MsgBox "Name : " & ActiveSheet.CheckBoxes(theboxes).Name
        Next theboxes
    End If
    End Sub
    Charlize

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @cjt can you please use code tags around the VBA-code ? Like

    Sub M_snb()
       with ActiveSheet.CheckBoxes.Add(Range("E6").Left, Range("E6").Top, Range("E6").Width, Range("E6").Height)
         .name="snb_001"
         .Caption = "All personal transactions have been properly Flagged"
       end with
    End Sub
    after that you can refer to those checkboxes with either
    sub M_snb_001()
       ActiveSheet.CheckBoxes(1).Visible = Not ActiveSheet.CheckBoxes(1).Visible
    end sub
    or
    Sub M_snb_002()
       ActiveSheet.CheckBoxes("snb_001").Visible = Not ActiveSheet.CheckBoxes("snb_001").Visible
    End Sub
    Last edited by snb; 12-04-2014 at 03:18 PM.

  6. #6
    VBAX Regular
    Joined
    Dec 2014
    Posts
    10
    Location
    Ok I will definitely use the code tags from now on, I'm new to this thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •