Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Edit cell based on position of option button clicked.

Basically I have a number of yes/no optionbuttons set up on an excel
worksheet. If no is selected on any of them I want the cell to the right of
the no option button to be activated. I can do everything i need to the cell
once it is activated but can't work out how to get it activated in the first
place. Ideally I want to produce a macro that will say:

when no option button clicked,
select cell 1 to right of optionbutton
edit cell

The problem being that there are lots of option buttons and I don't want to
have to create lots of macros with option button 1...etc.

I really hope someone can help as this is very frustrating, I am sure hat
there is an insanely simple answer!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Edit cell based on position of option button clicked.

Joseph,

It is clearly NOT an insanely simple answer. Here's what you need to do:

Insert a new Class module into your project, and note its name.

Put this code into the class module:

Public WithEvents OptionButtonGroup As MSForms.OptionButton

Private Sub OptionButtonGroup_Click()
Dim myOBCell As Range
Set myOBCell = Where(OptionButtonGroup.Left + _
OptionButtonGroup.Width, OptionButtonGroup.Top)
MsgBox "Hello from " & OptionButtonGroup.Name & _
" near cell " & myOBCell.Address
myOBCell.Select
End Sub


Then in a regular module, put this code:

Option Explicit
Dim OptionButtons() As New Class1 'use the name of the class here, if it is different

Sub SetupOBGroup()
Dim OptionButtonCount As Long
Dim OleObj As OLEObject
OptionButtonCount = 0
For Each OleObj In ActiveSheet.OLEObjects
If TypeOf OleObj.Object Is MSForms.OptionButton Then
OptionButtonCount = OptionButtonCount + 1
ReDim Preserve OptionButtons(1 To OptionButtonCount)
Set OptionButtons(OptionButtonCount).OptionButtonGroup = OleObj.Object
End If
Next OleObj
End Sub

Function Where(myLeft As Double, myTop As Double) As Range
Dim i As Integer
Dim myCol As Integer
Dim myRow As Long

'Find column
For i = 1 To 256
If Cells(1, i).Left myLeft Then
myCol = i
GoTo FoundCol
End If
Next i

FoundCol:

'Find Row
For i = 1 To 1000
If Cells(i, 1).Top myTop Then
myRow = i - 1
GoTo FoundRow
End If
Next i

FoundRow:

Set Where = Cells(myRow, myCol)
End Function

Then run the macro SetupOBGroup, and see what happens when you click the option buttons. You will
need to align your option button properly to get the desired effect.

HTH,
Bernie
MS Excel MVP


"Joseph Fletcher" wrote in message
...
Basically I have a number of yes/no optionbuttons set up on an excel
worksheet. If no is selected on any of them I want the cell to the right of
the no option button to be activated. I can do everything i need to the cell
once it is activated but can't work out how to get it activated in the first
place. Ideally I want to produce a macro that will say:

when no option button clicked,
select cell 1 to right of optionbutton
edit cell

The problem being that there are lots of option buttons and I don't want to
have to create lots of macros with option button 1...etc.

I really hope someone can help as this is very frustrating, I am sure hat
there is an insanely simple answer!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Edit cell based on position of option button clicked.

Thanks Bernie, I obviously need to learn how to use class modules!

A further question; this works perfectly when there is only one sheet with
buttons but not when there is more than 1. In my workbook I am going to have
a number of sheets (probably 6), all laid out in the same way. In my
worksheet_activate code I run setupOBGroup, this worked when there was the
single sheet but doesn't with more than one. If I run the code manually (by
F8-ing it) then it works, just not when VBA is shut. What part of the code
do I need to change, or how do I change my code to sort this out, either
resetting the class each time a different sheet is opened or by naming 6
different optionbutton classes.

Your help is invaluable,

Joe

"Bernie Deitrick" wrote:

Joseph,

It is clearly NOT an insanely simple answer. Here's what you need to do:

Insert a new Class module into your project, and note its name.

Put this code into the class module:

Public WithEvents OptionButtonGroup As MSForms.OptionButton

Private Sub OptionButtonGroup_Click()
Dim myOBCell As Range
Set myOBCell = Where(OptionButtonGroup.Left + _
OptionButtonGroup.Width, OptionButtonGroup.Top)
MsgBox "Hello from " & OptionButtonGroup.Name & _
" near cell " & myOBCell.Address
myOBCell.Select
End Sub


Then in a regular module, put this code:

Option Explicit
Dim OptionButtons() As New Class1 'use the name of the class here, if it is different

Sub SetupOBGroup()
Dim OptionButtonCount As Long
Dim OleObj As OLEObject
OptionButtonCount = 0
For Each OleObj In ActiveSheet.OLEObjects
If TypeOf OleObj.Object Is MSForms.OptionButton Then
OptionButtonCount = OptionButtonCount + 1
ReDim Preserve OptionButtons(1 To OptionButtonCount)
Set OptionButtons(OptionButtonCount).OptionButtonGroup = OleObj.Object
End If
Next OleObj
End Sub

Function Where(myLeft As Double, myTop As Double) As Range
Dim i As Integer
Dim myCol As Integer
Dim myRow As Long

'Find column
For i = 1 To 256
If Cells(1, i).Left myLeft Then
myCol = i
GoTo FoundCol
End If
Next i

FoundCol:

'Find Row
For i = 1 To 1000
If Cells(i, 1).Top myTop Then
myRow = i - 1
GoTo FoundRow
End If
Next i

FoundRow:

Set Where = Cells(myRow, myCol)
End Function

Then run the macro SetupOBGroup, and see what happens when you click the option buttons. You will
need to align your option button properly to get the desired effect.

HTH,
Bernie
MS Excel MVP


"Joseph Fletcher" wrote in message
...
Basically I have a number of yes/no optionbuttons set up on an excel
worksheet. If no is selected on any of them I want the cell to the right of
the no option button to be activated. I can do everything i need to the cell
once it is activated but can't work out how to get it activated in the first
place. Ideally I want to produce a macro that will say:

when no option button clicked,
select cell 1 to right of optionbutton
edit cell

The problem being that there are lots of option buttons and I don't want to
have to create lots of macros with option button 1...etc.

I really hope someone can help as this is very frustrating, I am sure hat
there is an insanely simple answer!




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Edit cell based on position of option button clicked.

Schoolboy. I'd switched off enable events accidently, your code works
perfectly. Thanks again.

"Joseph Fletcher" wrote:

Thanks Bernie, I obviously need to learn how to use class modules!

A further question; this works perfectly when there is only one sheet with
buttons but not when there is more than 1. In my workbook I am going to have
a number of sheets (probably 6), all laid out in the same way. In my
worksheet_activate code I run setupOBGroup, this worked when there was the
single sheet but doesn't with more than one. If I run the code manually (by
F8-ing it) then it works, just not when VBA is shut. What part of the code
do I need to change, or how do I change my code to sort this out, either
resetting the class each time a different sheet is opened or by naming 6
different optionbutton classes.

Your help is invaluable,

Joe

"Bernie Deitrick" wrote:

Joseph,

It is clearly NOT an insanely simple answer. Here's what you need to do:

Insert a new Class module into your project, and note its name.

Put this code into the class module:

Public WithEvents OptionButtonGroup As MSForms.OptionButton

Private Sub OptionButtonGroup_Click()
Dim myOBCell As Range
Set myOBCell = Where(OptionButtonGroup.Left + _
OptionButtonGroup.Width, OptionButtonGroup.Top)
MsgBox "Hello from " & OptionButtonGroup.Name & _
" near cell " & myOBCell.Address
myOBCell.Select
End Sub


Then in a regular module, put this code:

Option Explicit
Dim OptionButtons() As New Class1 'use the name of the class here, if it is different

Sub SetupOBGroup()
Dim OptionButtonCount As Long
Dim OleObj As OLEObject
OptionButtonCount = 0
For Each OleObj In ActiveSheet.OLEObjects
If TypeOf OleObj.Object Is MSForms.OptionButton Then
OptionButtonCount = OptionButtonCount + 1
ReDim Preserve OptionButtons(1 To OptionButtonCount)
Set OptionButtons(OptionButtonCount).OptionButtonGroup = OleObj.Object
End If
Next OleObj
End Sub

Function Where(myLeft As Double, myTop As Double) As Range
Dim i As Integer
Dim myCol As Integer
Dim myRow As Long

'Find column
For i = 1 To 256
If Cells(1, i).Left myLeft Then
myCol = i
GoTo FoundCol
End If
Next i

FoundCol:

'Find Row
For i = 1 To 1000
If Cells(i, 1).Top myTop Then
myRow = i - 1
GoTo FoundRow
End If
Next i

FoundRow:

Set Where = Cells(myRow, myCol)
End Function

Then run the macro SetupOBGroup, and see what happens when you click the option buttons. You will
need to align your option button properly to get the desired effect.

HTH,
Bernie
MS Excel MVP


"Joseph Fletcher" wrote in message
...
Basically I have a number of yes/no optionbuttons set up on an excel
worksheet. If no is selected on any of them I want the cell to the right of
the no option button to be activated. I can do everything i need to the cell
once it is activated but can't work out how to get it activated in the first
place. Ideally I want to produce a macro that will say:

when no option button clicked,
select cell 1 to right of optionbutton
edit cell

The problem being that there are lots of option buttons and I don't want to
have to create lots of macros with option button 1...etc.

I really hope someone can help as this is very frustrating, I am sure hat
there is an insanely simple answer!




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help on how to grey out one option button in one group box based on the selection of another option button in another group box George Excel Programming 12 March 11th 07 02:08 PM
How to edit the size and make it bold of the Option Button text? Cammie Excel Discussion (Misc queries) 1 February 23rd 05 01:57 AM
GoTo Worksheet based on which button clicked Paul Martin Excel Programming 2 July 6th 04 06:22 AM
Control Cell Link for Option Button based on value in a cell arunjoshi[_14_] Excel Programming 1 May 5th 04 02:19 AM
Control Cell Link for Option Button based on value in a cell arunjoshi[_13_] Excel Programming 0 May 4th 04 05:46 AM


All times are GMT +1. The time now is 04:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"