![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Mar 2002
Posts: 142
|
Here's what I'm using to make three cells act as a group of radio buttons, making it so the user can only use one of the three cells at a time:
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$AD$133" Then [AD134,AD135].ClearContents If Target.Address = "$AD$134" Then [AD133,AD135].ClearContents If Target.Address = "$AD$135" Then [AD133,AD134].ClearContents End Sub HERE'S MY PROBLEM I tried to add two more cells to this structure, but it didn't work. Here's what I tried: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$AD$133" Then [AD134,AD135].ClearContents If Target.Address = "$AD$134" Then [AD133,AD135].ClearContents If Target.Address = "$AD$135" Then [AD133,AD134].ClearContents If Target.Address = "$AD$63" Then [AD64].ClearContents If Target.Address = "$AD$64" Then [AD63].ClearContents End Sub Any info on how to add these two cells (AD63 and AD64) to this sub so that they relate to eachother in the same way the other three cells relate to eachother? Thanks in advance. |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
Hi Bobmc,
What you have here is a classic case of infinite recursion. In your code If Target.Address = "$AD$63" Then [AD64].ClearContents If Target.Address = "$AD$64" Then [AD63].ClearContents when [AD64] is cleared, this triggers the Change event again, and this time it is [AD64] that has changed, so when it then clears [AD63] the Change event is again triggered, and so on ad infinitum. I honestly don't understand why this same problem is not occurring when you have the set of three cells--it sure looks like it should to me. Anyway, if this is the problem there is an easy solution. Simply set a flag that indicates that recursion is occurring and jump out of the routine: Private Sub Worksheet_Change(ByVal Target As Range) Static Recursive As Boolean If Recursive Then Exit Sub Recursive = True If Target.Address = "$AD$133" Then [AD134,AD135].ClearContents If Target.Address = "$AD$134" Then [AD133,AD135].ClearContents If Target.Address = "$AD$135" Then [AD133,AD134].ClearContents If Target.Address = "$AD$63" Then [AD64].ClearContents If Target.Address = "$AD$64" Then [AD63].ClearContents Recursive = False End Sub And incidentally, here is a more efficient way of doing your tests that doesn't continue testing the other cells even after the target has been found: Private Sub Worksheet_Change(ByVal Target As Range) Static Recursive As Boolean If Recursive Then Exit Sub Recursive = True Select Case Target.Address Case "$AD$133" :[AD134,AD135].ClearContents Case "$AD$134" : [AD133,AD135].ClearContents Case "$AD$135" :[AD133,AD134].ClearContents Case "$AD$63" :[AD64].ClearContents Case "$AD$64" :[AD63].ClearContents End Select Recursive = False End Sub
__________________
Keep Excelling. Damon VBAexpert Excel Consulting (My other life: http://damonostrander.com ) |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|