PDA

View Full Version : Solved: option button help



prasadksu
03-29-2009, 12:46 PM
hello everyone,

I am new to VBA programming. I created an user form and there are three option buttons (daily, monthly and yearly) grouped in a frame so that the user can choose only one of the options. For example if the user chooses daily button, then in cell A1, 1 should be printed and if the user chooses monthly option, then in cell A2, 30 should print and for yearly option, in cellA3, 360 should print. I tried doing using the following code but i was not succesful....

Private Sub CommandButton1_Click()

If optdaily = True Then
ThisWorkbook.Worksheets("sheet1").Range("A1").Value = "1"

ElseIf optmonthly = True Then

ThisWorkbook.Worksheets("sheet1").Range("A2").Value = "30"
Else
ThisWorkbook.Worksheets("sheet1").Range("A3").Value = "360"

End If
End Sub

I would be verythankful if anyone could correct the code......

Thanks,
Prasad.

nst1107
03-29-2009, 02:23 PM
Welcome to the forum, Prasad. Your code looks pretty good. In fact, it works fine for me. Without seeing the rest of your project and knowing your objectives, I can only speculate on why it's not doing what you want.

If the user has more than one chance to click CommandButton_1, then I suggest adding a line to clear the values in the range [A1:A3] prior to entering a new value based on the option buttons, unless, of course, it's okay to have more than one of the cells in that range filled.

If all three option buttons are not in their own group, then you may have conflicts with other option buttons in your userform, or you may run into problems with more than one of the option buttons being selected at once.

If you have not set the value of one of the three option buttons to true at design time, then you may have unexpected behavior if no option buttons have been clicked before CommandButton_1 is clicked.

That's about all I can think of to explain why it's not doing what you want. Hope it helps.

prasadksu
03-29-2009, 02:40 PM
Thanks a lot for your reply. But its not working for me. I am attaching the excel sheet... please have a look into it and correct my code....

thanks,
Prasad.

tpoynton
03-29-2009, 05:30 PM
your optionbutton names on the userform are not aligned with what is in the code. Is this homework?

prasadksu
03-29-2009, 06:58 PM
Thanks a lot for your help........Its working now...........Its not for a Homework assignment.......It is a piece of a processing tool that I am thinking to develop

:thumb:thumb

Thanks
Prasad