Search code examples
excelvbaactivex

VBA Option Buttons Clearing for Unknown Reason


I’m building off a workbook someone created years ago where Sheet 1 has primary equipment info entered into ActiveX text boxes and option buttons, with matching ActiveX controls below it for secondary equipment to do calculations. Sheet 2 & 3 copy the primary equipment info over into ActiveX label boxes to do calculations for other equipment.

The code was set up like below to copy the primary equipment info to sheets 2 & 3.

Private Sub Worksheet_Activate()
If Right(ActiveSheet.Name, 1) <> "1" Then
   Sheet2.label1.Text = Sheet1.textbox1.Text
   Sheet2.label2.Text = Sheet1.textbox2.Text
   Sheet2.opt1.Value = Sheet1.opt1.Value
   Sheet2.opt2.Value = Sheet1.opt2.Value
End If
End Sub

I added another sheet to do calculations for another piece of equipment, using matching VBA, however, when I click from Sheet 4 back to Sheet 1 (with the original option buttons), the Sheet 1 option button values are always cleared. It does NOT happen on the other 2 existing sheets, just my new one. I’ve narrowed it down to this particular sub (on sheet 4) that is causing the issues, but can’t figure out how to resolve it.

I’ve tried the following variations without success in stopping the option buttons on sheet 1 from clearing.

  • changing Private Sub to a command button ( Command_Click() )
  • replacing If statement with With Me
  • removing If/With Me statement alltogether

There isn’t any other code that clears these particular option buttons, other than in ThisWorkbook for a Workbook_Open() sub (and deleting that didn’t make a difference). Does anyone have an idea for what may be causing this, or VBA code to prevent it from happening?

I made a test workbook without sensitive data, and the issue happens after clicking on each of the sheets (2-4) and then clicking back to sheet1 (so slightly different than in the original workbook). I also don't see any differences in the control properties. OneDrive link: https://1drv.ms/x/c/e4891d1fec732162/EeyXfmzcETlGjNDy7_St01EBUbOdZOTDH9U3SbMpPSM3kQ?e=HXCO93

I would like to keep the option buttons on each sheet since it will be a pain to update the VBA on following sheets (+ aesthetics).

This is using Excel from Office 365 for Business.

Thanks!


Solution

  • I added a new option button for testing and it didn't seem to have the same problem.

    Then I noticed I'd not set the 'GroupName" for the new option button. When I did that, the behavior reverted to what you're seeing.

    Seems like the option button GroupName is somehow being applied across worksheets, which (I thought) shouldn't be the case.

    Confirmed that in a new workbook with a single option button on each of two sheets and the same GroupName assigned to each one - clicking on one clears the other if it was set. So - you need different group names for each set of related option buttons on each sheet.

    Edit - not a new thing... From 2005: https://www.excelforum.com/excel-programming-vba-macros/476859-groupname-for-optionbutton.html