VBA Userform Data to be saved on Excel Sheet

excel_sp

New Member
Joined
Feb 25, 2018
Messages
9
Hello all,

I am trying to create an excel VBA userform which has a few details that are input using textboxes like "Student Name", "Student Address", Student contact number" etc. This form also contains several questions for which the user is supposed to select one of the 5 optionbuttons coressponding to wach question which are placed in a container frame.

There is a command button to save the input from the userform to the next excel sheet tab.

I am using the following piece of code:

Code:
Set ws = Worksheets("Data")
irow = ws.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Range("A" & irow) = TextBox5.Value
.Range("B" & irow) = TextBox6.Value

The above piece of code is working. However I am not sure how to save the data from the chosen optionbutton in excel sheet.

Please help this newbie out!

:)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
    Dim i As Long
    For i = 1 To 5
        If Controls("OptionButton" & i) = True Then
            [COLOR=#ff0000]Range("A1")[/COLOR] = "Option " & i & " selected."
        End If
    Next

Change the red address to where you want it inserted
 
Upvote 0
Thank you Paul for your quick response. I have made some progress with the help of the code snippet that you had shared.

Now let's say:

Code:
OptionButton1 = 0
OptionButton2 = 2.5
OptionButton3 = 6
OptionButton4 = 7
OptionButton5 = 9
OptionButton6 = " "

       For i = 1 To 5
            If Controls("OptionButton" & i) = True Then

            .Range("H" & irow) = ("OptionButton" & i)
        End If
    Next

The output that I am getting in the excel sheet is basically the name of the Optionbutton that I have selected. How should i change the code to get the value of the selected Optionbutton in the cell.

Thanks again.
 
Upvote 0
OptionButtons are boolean, you'll have to create the text in your sub when writing to the sheet.
 
Upvote 0
Sorry, had to make a phone call...

Code:
    If OptionButton1 Then Range("H" & irow) = "0"
    If OptionButton2 Then Range("H" & irow) = "2.5"
    If OptionButton3 Then Range("H" & irow) = "6"
    If OptionButton4 Then Range("H" & irow) = "7"
    If OptionButton5 Then Range("H" & irow) = "9"
 
Last edited:
Upvote 0
Thank you so much! Was able to make the necessary changes with your help in the userform to get the desired results!:biggrin::bow:
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top