[VBA] trying to make function for option group

Srbin do jaja

New Member
Joined
Sep 5, 2011
Messages
7
here is the code i have and it works as a charm
Code:
Private Sub CommandButton2_Click()
    Dim LastRow As Object
    Dim totalRows As Integer
    Dim i As Integer
    Dim x As Integer

    Set LastRow = Sheet2.Range("B65536").End(xlUp)

    If OptionButton1.Value = True Then
        LastRow.Offset(1, 1).Value = OptionButton1.Caption
    ElseIf OptionButton2.Value = True Then
        LastRow.Offset(1, 1).Value = OptionButton2.Caption
    ElseIf OptionButton1.Value = False And OptionButton2.Value = False Then
        MsgBox "Answer the question: Kog ste pola"
        Exit Sub
    End If
End Sub

but i have 10 option groups with 4 option button in each i was trying to make a function out of above example to:
Code:
Private Sub getOptBtnValue()

    x = 0
    For i = 12 To 16
    x = x + 1
    If Controls("OptionButton" & i) = True Then
        LastRow.Offset(1, 4).Value = Controls("OptionButton" & i).Caption
           Exit For
    End If
  
    If x = 5 Then
       MsgBox "Answer the question: Koliko ukupno imate radnog iskustva?"
       Exit Sub
    End If
    
    Next i
    
End Sub

when i run this function it gives me an error in line where
Code:
LastRow.Offset(1, 4).Value = Controls("OptionButton" & i).Caption
what am i doing wrong?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The first code you posted has this line in it...

Code:
Set LastRow = Sheet2.Range("B65536").End(xlUp)
You do not have it in your own code, but your code still refers to the LastRow cell.
 
Upvote 0
The first code you posted has this line in it...

Code:
Set LastRow = Sheet2.Range("B65536").End(xlUp)
You do not have it in your own code, but your code still refers to the LastRow cell.

i forgot to say that after this
Code:
Private Sub CommandButton2_Click()
    Dim LastRow As Object
    Dim totalRows As Integer
    Dim i As Integer
    Dim x As Integer

    Set LastRow = Sheet2.Range("B65536").End(xlUp)

i call sub getOptBtnValue
 
Upvote 0
But getOptBtnValue doesn't know anything about LastRow because it is declared inside the CommandButton2_Click event procedure. Where is the getOptBtnValue code located... in the sheet module with the CommandButton2 Click event code or in a standard module (where macros are installed)?
 
Upvote 0
well i'm novice in VBA programming, where should i declare getOptBtnValue. i'm super confused. right now its in function in side sheet2.
Code:
Private Sub CommandButton2_Click()
    Dim LastRow As Object
    Dim totalRows As Integer
    Dim i As Integer
    Dim x As Integer

    Set LastRow = Sheet2.Range("B65536").End(xlUp)

    If OptionButton1.Value = True Then
        LastRow.Offset(1, 1).Value = OptionButton1.Caption
    ElseIf OptionButton2.Value = True Then
        LastRow.Offset(1, 1).Value = OptionButton2.Caption
    ElseIf OptionButton1.Value = False And OptionButton2.Value = False Then
        MsgBox "Answer the question: Kog ste pola"
        Exit Sub
    End If
End Sub

this works. since i have 5 more opt groups i dont want to write same code for every opt group. what would be a solution?
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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