Results 1 to 2 of 2

Thread: Problem with looping through checkboxes in Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Problem with looping through checkboxes in Excel

    Hi

    I'm trying to get the following to work;

    VB Code:
    1. For x = 1 To 28
    2. For Each CheckBox(x) In sSheet
    3.     If Not CheckBox(x) = True Then
    4.         For i = 6 To Sheet3.Cells(1, 1).End(xlToRight).Column
    5.             If Sheet3.Cells(1, i).Value = CheckBox(x).Caption Then
    6.                 Sheet3.Columns(i).EntireColumn.Delete
    7.             End If
    8.         Next i
    9.     End If
    10. Next CheckBox
    11. Next x

    however this doesn't work - any alternatives?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with looping through checkboxes in Excel

    You need to loop through the collection of OLEObjects in the worksheet . I use a naming convention whereby all checkboxes are prefixed with "chk" which allows me to identify the specific type of control.

    VB Code:
    1. Dim sSheet As Worksheet
    2. Dim oControl As OLEObject
    3.    
    4.     Set sSheet = Worksheets(1)
    5.    
    6.     For Each oControl In sSheet.OLEObjects
    7.         With oControl
    8.             If Left(.Name, 3) = "chk" _
    9.             And .Object.Value = True Then
    10.                 'Rest of your code goes here
    11.             End If
    12.         End With
    13.     Next oControl
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width