Not sure if this should be in a new thread or in this one. I took the code that rylo provided and used that perfectly. What I am trying to do is as follows: After the form is populated with the command buttons and the user presses the button sheet1. I have a routine that takes that sheet1 name value and holds it to be referenced later.
Sub GetCmdCaption()
CmdBtn = ButtonGroup.Name
Select Case CmdBtn
Case CmdBtn
frmMain_Menu.Hide
UserForm1.Show
End Select
End Sub
I then use that value on Userform1 to tell it what sheet in the workbook to look at. What I would like to have happen is depending on the button on the main menu that is clicked the next form would auto populate option buttons with non repeating values found in F2 for the option name and caption. For Example in the sheets F2 holds values like belt, chain, valve, misc, etc. The code below is what I have come up with based on a deriviative of rylo code and other refrences from this forum, but I have hit a wall and I am hoping that you all might be able to help. The lines of code in red are where I have lost myself...and I am pretty sure where my biggest hangup is.
Example:
Dim Options() As New Class2
Private Sub UserForm_Initialize()
Dim lr As Variant
Dim Wks As Worksheet
Dim Rng As Range
Dim rngData As Range
Dim rngCell As Range
Set Wks = Worksheets(CmdBtn)
Set Rng = Wks.Range("F2")
Dim colWords As Collection
Dim vntWord As Variant
lr = Range("F" & Rows.Count).End(xlUp).Row
Set colWords = New Collection
Set rngData = Range("F2:F" & lr)
For Each rngCell In rngData.Cells
NextDim i As Long, MyCmd As Control, Topper As Long, Lefter As Long, widther As Long, heighter As Long
Dim OptCount As Integer, ctl As Control
OptCount = 1
Lefter = 10
Topper = 3
widther = 100
heighter = 25
For i = 1 To lr
Set MyOpt = UserForm2.Controls.Add(bstrprogID:="Forms.OptionButton.1", Name:=lr(i).Cell, Visible:=True) MyOpt.Top = Topper
MyOpt.Left = Lefter
MyOpt.Caption = lr(i).Name
MyOpt.Width = widther
MyOpt.Height = heighter
Lefter = Lefter + 125
If Lefter > 440 Then
Topper = Topper + 30
Lefter = 10
End If
Next i
For Each ctl In UserForm2.Controls
If TypeName(ctl) = "OptionButton" Then
OptCount = OptCount + 1
ReDim Preserve Options(1 To OptCount)
Set Options(OptCount).OptionGroup = ctl
End If
Next ctl
End Sub
Class Module:
Public WithEvents OptionGroup As msforms.OptionButton
Private Sub OptionGroup_Click()
MsgBox "Hello from " & OptionGroup.Caption
End Sub
Thanks in advance for your help.
Bob
Bookmarks