Results 1 to 3 of 3

Thread: looping optionbuttons in userform??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    looping optionbuttons in userform??

    Hi all

    I've added a currency converter (as a pop-up userform) to one of my existing macros. At first I only used 1 base value (eg. it could only convert USD -> EUR, not the other way). Now I would like to make it go both ways - and I have 5 different currencies.

    What I thought would work is this;

    VB Code:
    1. Private Sub CommandButton1_Click()
    2. Dim x As Long
    3. Dim i As Long
    4. Dim j As Long
    5. Dim vSheet As Worksheet
    6. Dim dSheet As Worksheet
    7. Dim oOption As MSForms.OptionButton
    8.  
    9. Set vSheet = ActiveWorkbook.Worksheets("Valuta")
    10. Set dSheet = ActiveWorkbook.Worksheets("Data")
    11.  
    12. Application.ScreenUpdating = False
    13.  
    14. For Each oOption In FrmCur.Frame1
    15.     With oOption
    16.         If .Value = True Then
    17.             For x = 2 To 6
    18.                 If Opt1.Caption = vSheet.Cells(x, 1).Value Then
    19.                 With vSheet
    20.                     For i = 6 To dSheet.Cells(1, 5).End(xlToRight).Column
    21.                     If Not Right(.Cells(1, i).Value, 3) = "pct" Then
    22.                                 vSheet.Activate
    23.                                 vSheet.Cells(x, 2).Copy
    24.                                 dSheet.Activate
    25.                                 dSheet.Range(Cells(2, i), Cells(2, i).End(xlDown)).PasteSpecial Paste:=xlValues, operation:=xlMultiply
    26.                                
    27.                     End If
    28.                     Next i
    29.                 End With
    30.                 End If
    31.             Next x
    32.         End If
    33.     End With
    34. Next oOption
    35.  
    36. For Each oOption In FrmCur.Frame2
    37.     With oOption
    38.         If .Value = True Then
    39.             For x = 2 To 6
    40.                 If Opt1.Caption = vSheet.Cells(x, 1).Value Then
    41.                 With vSheet
    42.                     For i = 6 To dSheet.Cells(1, 5).End(xlToRight).Column
    43.                     If Not Right(.Cells(1, i).Value, 3) = "pct" Then
    44.                                 vSheet.Activate
    45.                                 vSheet.Cells(x, 2).Copy
    46.                                 dSheet.Activate
    47.                                 dSheet.Range(Cells(2, i), Cells(2, i).End(xlDown)).PasteSpecial Paste:=xlValues, operation:=xlDivide
    48.                                
    49.                     End If
    50.                     Next i
    51.                 End With
    52.                 End If
    53.             Next x
    54.         End If
    55.     End With
    56. Next oOption
    57.  
    58. FrmCur.Hide
    59.  
    60. Application.ScreenUpdating = True
    61.  
    62. End Sub

    However this line;
    VB Code:
    1. For Each oOption In FrmCur.Frame2
    is giving me problems. I know I can use it on a spreadsheet (browsing OLEObjects) - so can't I also use in a userform?

    Any thoughts are welcome

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: looping optionbuttons in userform??

    direktoren

    When looping through any set of controls in a frame you must use the Controls collection for the frame. This means that you will need to Dim your object variable generically as a control rather than specifically as an OptionButton. You can use the TypeName function to then limit to the type of control you want the loop to handle.

    For example, this piece of code will not work
    VB Code:
    1. Dim oOpt As OptionButton
    2.    
    3.     For Each oOpt In Me.Frame1.OptionButtons
    4.         'Do some stuff
    5.     Next oOpt
    But this code will
    VB Code:
    1. Dim oCont As Control
    2.  
    3.     For Each oCont In Me.Frame1.Controls
    4.         If TypeName(oCont) = "OptionButton" Then
    5.             'Do some stuff
    6.         End If
    7.     Next oCont

    So in your code, you will need to change the DIM statement for the oOption object to
    VB Code:
    1. Dim oOption As MSForms.Control
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: looping optionbuttons in userform??

    Hi Declan

    Thanks for the quick response - as usual...and as usual I must have done something wrong again! I don't get any errors now, but nothing happens either I think this piece might be causing the nothingness to happen!;
    VB Code:
    1. For x = 2 To 6
    2.                 If .Caption = vSheet.Cells(x, 1).Value Then
    3.                 With vSheet
    4.                     For i = 6 To dSheet.Cells(1, 5).End(xlToRight).Column
    5.                     If Not Right(.Cells(1, i).Value, 3) = "pct" Then
    But I can't see what the problem should be... Perhaps the
    VB Code:
    1. .caption = vSheet.Cells(x,1).value
    part?

    Nicolaj

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width