ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Programmely written code don't work (https://www.excelbanter.com/excel-programming/293067-programmely-written-code-dont-work.html)

Flemming Dahl[_2_]

Programmely written code don't work
 
Hi,

On a userform certain actions course extra controls AND extra event-code for
thise controls - however the code looks right........... it will not
work......

A OptionButton is created "OptionButton1" and this code is made one the
userform's code module:
Private Sub OptionButton1_Click()
MsgBox "YES"
End Sub

What do i do to make the new code work ?

Thanks,
Flemming



Keith R[_3_]

Programmely written code don't work
 
Works for me in OfficeXP.
Are you trying to get the msgbox within the VBA window, or are you actually
bringing up the Userform in Excel and clicking the optionbutton there? Do
you have macros enabled?
Keith

"Flemming Dahl" wrote in message
...
Hi,

On a userform certain actions course extra controls AND extra event-code

for
thise controls - however the code looks right........... it will not
work......

A OptionButton is created "OptionButton1" and this code is made one the
userform's code module:
Private Sub OptionButton1_Click()
MsgBox "YES"
End Sub

What do i do to make the new code work ?

Thanks,
Flemming





Flemming Dahl[_2_]

Programmely written code don't work
 
The code that is create in runtime is created like this:

Sub AddButtonsCode()
Dim UFvbc As Object 'VBComponent
Dim code As String
Dim n As Long
Set UFvbc = ThisWorkbook.VBProject.VBComponents("UserForm1")
For n = 1 To 10
With UFvbc.CodeModule
code = ""
code = code & "Private Sub OptionButton" & n & "_Click" & vbCr
code = code & "Msgbox ""YES - OptionButton" & n & """" & vbCr
code = code & "End Sub"
.InsertLines .CountOfLines + 1, code
End With
Next
End Sub





"Flemming Dahl" wrote in message
...
Hi,

On a userform certain actions course extra controls AND extra event-code

for
thise controls - however the code looks right........... it will not
work......

A OptionButton is created "OptionButton1" and this code is made one the
userform's code module:
Private Sub OptionButton1_Click()
MsgBox "YES"
End Sub

What do i do to make the new code work ?

Thanks,
Flemming





Keith R[_3_]

Programmely written code don't work
 
Then you have stepped outside the bounds of my experience- I'm just starting
to figure out how to add controls at runtime in .net :-)

Just out of curiosity, have you considered the option of creating the
controls (assuming there is a limited number) and just showing/hiding (and
possibly moving) them based on the option button events?

"Flemming Dahl" wrote in message
...
Hey Keith

Looking at you answer made me think, that i have not explained my self
properly.

You see - the userform IS running, and by selection on a combobox - NEW
controls are added to the userform, and so is the event code for thise
controls. But no event takes place for the new controls.

Here a NEW control is a control that is created in runtime.

Flemming


"Keith R" wrote in message
...
Works for me in OfficeXP.
Are you trying to get the msgbox within the VBA window, or are you

actually
bringing up the Userform in Excel and clicking the optionbutton there?

Do
you have macros enabled?
Keith

"Flemming Dahl" wrote in message
...
Hi,

On a userform certain actions course extra controls AND extra

event-code
for
thise controls - however the code looks right........... it will not
work......

A OptionButton is created "OptionButton1" and this code is made one

the
userform's code module:
Private Sub OptionButton1_Click()
MsgBox "YES"
End Sub

What do i do to make the new code work ?

Thanks,
Flemming









Flemming Dahl[_2_]

Programmely written code don't work
 

Yes - have considered show/hide......... but that will not give the
flexibility that i need.



Bob Phillips[_6_]

Programmely written code don't work
 
Flemming,

That would be my approach. What flexibility does it not provide?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Flemming Dahl" wrote in message
...

Yes - have considered show/hide......... but that will not give the
flexibility that i need.





Flemming Dahl[_2_]

Programmely written code don't work
 

Show/Hide dos not provide the flexiblity of how many controls the user
needs.
In this case i am not going to make alle those controls and show/hide
procedures..........

Lets stop it here. It seems that you can not provide any extra information
to solve my problem.

Thanks,
Flemming



Bob Phillips[_6_]

Programmely written code don't work
 
Flemming,

How about a different approach, namely to create a class module that can
handle all optionbutton controls.

First, create a class module, and name it clsOpt, with this code

Public WithEvents OptButton As MSForms.OptionButton

Private Sub OptButton_Click()
'add your specific code here
MsgBox "You clicked: " & OptButton.Caption
End Sub

Then in your userform event, add this code

Dim collOpt As Collection
Dim formButton As clsOpt

Private Sub UserForm_Initialize()
Dim oCtl As MSForms.Control
Set collOpt = New Collection
For Each oCtl In Me.Controls
If TypeOf oCtl Is MSForms.OptionButton Then
Set formButton = New clsOpt
Set formButton.optButton = oCtl
collOpt.Add formButton
End If
Next oCtl
End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Flemming Dahl" wrote in message
...

Show/Hide dos not provide the flexiblity of how many controls the user
needs.
In this case i am not going to make alle those controls and show/hide
procedures..........

Lets stop it here. It seems that you can not provide any extra information
to solve my problem.

Thanks,
Flemming





Flemming Dahl[_2_]

Programmely written code don't work
 
Thanks Bob

This Class module I gotta try :)

Flemming




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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com