I have an Excel sheet set-up with four radio buttons (option buttons) inside of a groupbox. It looks like this: Excel Sheet Setup.
The following code should return the caption of the groupbox ("Buttons").
Sub rad_change2()
Dim button As OptionButton
Set button = ActiveSheet.OptionButtons.Item(2)
Debug.Print (button.GroupBox.Caption)
End Sub
When the code runs, there is an error on the Debug.Print line. When this happens, I can go into the locals window, and click the [+] icon to expand the properties for button. After expanding the properties in the locals window, I can press resume on the code and the code finishes running as it should and returns the caption of the groupbox.
What do I need to do to get the code to run without getting an error? Why does expanding the object properties in the local windows resolve the error? The code seems to be correct, as it does do what I need it to do, but I need it to do so without any errors in the process.
I'm using the Excel through Office 365, version 2003 (build 12624.20320).
Along with expanding the button properties to get the code to finish working after a breakpoint, I discovered that hovering over the button variable to preview its value also works. This had me wondering if the value or a property of the button needed to be accessed before I tried to get the caption property of the groupbox property. I'm not sure why I would have to do this, but it ended up working.
The following code does the trick.
Sub rad_change2()
Dim tempString As String
Dim button As OptionButton
Set button = ActiveSheet.OptionButtons.Item(2)
tempString = button.Name
Debug.Print (button.GroupBox.Caption)
End Sub
Instead of using a variable tempString, the button.Name
could be displayed with a messagebox or Debug.Print
for the same effect.