0

How can I use my variable from sub procedure to add specific items for comboBox in userForm?

Sub procedure (Module code) -

Dim main As Integer

Public Sub dataValidation()
    For i = 3 To 22
        If Cells(i, 7).Value = "" Then
            If Cells(i, 6).Value = "x" Then
                main = 1
            ElseIf Cells(i, 6).Value = "y" Then
                main = 2
            ElseIf Cells(i, 6).Value = "z" Then
                main = 3
            End If
            form.Show
        End If
    Next
End Sub

Form initialization (Form code) -

Private Sub UserForm_Initialize()
    cboSubtype.Value = "Select subtype"
    if main = 1 then
        cboSubtype.AddItem "a"
        cboSubtype.AddItem "s"
    elseif main = 2 then
        cboSubtype.AddItem "d"
        cboSubtype.AddItem "f"
    elseif main = 3 then
        cboSubtype.AddItem "g"
        cboSubtype.AddItem "h"
End Sub
2
  • you need both Subs to recognixe the same main, so in your first line use Public main As Integer. You should learn how to use Select Case instaed of multiple If >> Else . Commented Jan 8, 2017 at 14:55
  • Ideally for passing values to a form and using the user's selection in your code, you create an instance of the form, pass the values to it and retrieve the selected value when the OK button of the form is pressed. This post of mine has a sample workbook you can download: yoursumbuddy.com/a-flexible-vba-chooser-form. The best explanation is in Professional Excel Development by Stephen Bullen, et. al. Commented Jan 8, 2017 at 16:32

2 Answers 2

2

The code below is using the methods you wanted, to pass a variable main from module to User_Form init event.

Note: You could perform is all in 1 code (inside the user_form init event).

Sub dataValidation Code (Module)

Option Explicit

Public main As Integer

Public Sub dataValidation()

Dim i As Integer

For i = 3 To 22
    If Cells(i, 7).Value = "" Then
        Select Case Cells(i, 6).Value
            Case "s"
                main = 1

            Case "y"
                main = 2

            Case "z"
                main = 3

        End Select
        form.Show
    End If
Next i

End Sub

Sub User_Form Code (on init event)

Private Sub UserForm_Initialize()

With cboSubtype
    .Value = "Select subtype"

    Select Case main
        Case 1
            .AddItem "a"
            .AddItem "s"

        Case 2
            .AddItem "d"
            .AddItem "f"

        Case 3
            .AddItem "g"
            .AddItem "h"

    End Select
End With

End Sub
Sign up to request clarification or add additional context in comments.

8 Comments

It works. As far as i understood variable can be passed to form init event because of Select case statement, am I right?
the value in your case is not passed, but it's a Public variable that all modules recognize. You can keep your If and Else but why would you ?
Got it. There's no reason to stick with If else from now on =] I'm struggling for a while with a new issue, maybe you can help - How can I take value from SelectBox and place it in cell?
with your code, since you have more than **2(( Ifs it's easier and makes more sense to use Select Case. In other scenarios, where you have 2 or less Ifs then it makes more sense keeping the If. Am I being clear now or made more of a mess ?
It seems like you misunderstood my question, please, check last respond to this question.
|
1
Private Sub UserForm_Initialize()
this = "this": that = "that": thenext = "thenext"
With ComboBox1
    .AddItem this
    .AddItem that
    .AddItem thenext
End With
End Sub

This compiles for me.

3 Comments

On line 3 you add combo box to user form or select it to add items?
One more question - when I use UserForm1.Show inside my module code it ignores code inside Private Sub UserForm_Initialize() and pop form without any combo box items.
@maksem the general syntax for adding items to comboboxes/listboxes is with ".additem." I just use "With" because i think it looks cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.