Share via

Looping through multiple Optionbuttons

Anonymous
2010-07-27T20:06:15+00:00

I have 170 optionbuttons on a worksheet.  When the user opens the workbook, I want a macro to execute and loop through all option buttons and set the value to FALSE, thereby deselecting any that my have been selected so the user will have a black slate, so to speak.

This is the code that I currently have but it has an error:

Private Sub Workbook_Open()

   Dim k As Integer

   Sheets("Template").Activate

   For k = 1 To 170

      ActiveSheet.OLEObjects("Optionbutton" & k).Value = False

   Next k

End Sub

Thanks for your help,

Les

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-07-27T21:42:17+00:00

Option Explicit

Private Sub Workbook_Open()

   Dim k As Long

   'WorkSheets("Template").Activate

   For k = 1 To 170

      Worksheets("template").OLEObjects("Optionbutton" & k).Object.Value = False

   Next k

End Sub

There's no reason to activate the Template worksheet (for this anyway).

WLMPilot wrote:

I have 170 optionbuttons on a worksheet.  When the user opens the workbook, I want a macro to execute and loop through all option buttons and set the value to FALSE, thereby deselecting any that my have been selected so the user will have a black slate, so to speak.

This is the code that I currently have but it has an error:

Private Sub Workbook_Open()

   Dim k As Integer

   Sheets("Template").Activate

   For k = 1 To 170

      ActiveSheet.OLEObjects("Optionbutton" & k).Value = False

   Next k

End Sub

Thanks for your help,

Les

--

Dave Peterson

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2010-07-27T20:12:48+00:00

    Hi,

    How about this

    Dim K As Long

    Sheets("Template").Activate

    For K = 1 To ActiveSheet.OLEObjects.Count

       If TypeName(ActiveSheet.OLEObjects(K).Object) = "OptionButton" Then

        ActiveSheet.OLEObjects(K).Object = False

      End If

    Next K


    If this post answers your question, please mark it as the Answer.

    Mike H

    Was this answer helpful?

    0 comments No comments