Closed Thread
Results 1 to 9 of 9

Need Help with VBA code that populates a dropdown in a userform conditionally

Hybrid View

seannormand Need Help with VBA code that... 11-06-2014, 02:40 PM
xlbiznes Re: Need Help with VBA code... 11-06-2014, 03:04 PM
seannormand Re: Need Help with VBA code... 11-06-2014, 05:15 PM
seannormand Re: Need Help with VBA code... 11-06-2014, 05:02 PM
xlbiznes Re: Need Help with VBA code... 11-06-2014, 05:06 PM
xlbiznes Re: Need Help with VBA code... 11-06-2014, 05:26 PM
seannormand Re: Need Help with VBA code... 11-06-2014, 05:39 PM
xlbiznes Re: Need Help with VBA code... 11-06-2014, 06:24 PM
zbor Re: Need Help with VBA code... 11-10-2014, 09:08 AM
  1. #1
    Registered User
    Join Date
    01-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    83

    Need Help with VBA code that populates a dropdown in a userform conditionally

    Hello Excel Forum!

    I have some VBA code that populates a userform based on text and named ranges. The userform is a survey for clients to take and it works great. The only problem is that it's set for 8 responses (a dropdown selection and text entry). It's really easy to change the question in the survey and the possible responses in the dropdown but the problem is if i want to go from 8 questions to 7 then I have to go in and delete a bunch of code because the code tells it to populate the userform based on text in certain places. What I would like is a conditional statement which says "if there is text in that cell then use it" if there isn't anything there no big deal. Here's what I have. I would really appreciate any help.

    Thanks,

    Private Sub UserForm_Initialize()
    Question1.Caption = Worksheets("Day 1 Answers").Range("C1").Value
    Question2.Caption = Worksheets("Day 1 Answers").Range("E1").Value
    Question3.Caption = Worksheets("Day 1 Answers").Range("G1").Value
    Question4.Caption = Worksheets("Day 1 Answers").Range("I1").Value
    Question5.Caption = Worksheets("Day 1 Answers").Range("K1").Value
    Question6.Caption = Worksheets("Day 1 Answers").Range("M1").Value
    Question7.Caption = Worksheets("Day 1 Answers").Range("O1").Value
    Question8.Caption = Worksheets("Day 1 Answers").Range("Q1").Value
    
    Combobox1.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)
    Combobox2.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_2").RefersToRange)
    combobox3.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_3").RefersToRange)
    ComboBox4.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_4").RefersToRange)
    ComboBox5.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_5").RefersToRange)
    ComboBox6.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_6").RefersToRange)
    ComboBox7.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_7").RefersToRange)
    ComboBox8.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_8").RefersToRange)
    
    
    
    End Sub
    Private Sub Survey_Initialize()
    'Empty FirstNametxt
    FirstNametxt.Value = ""
    
    'Empty LastNametxt
    LastNametxt.Value = ""
    
    'Set Focus on FirstNametxt
    FirstNametxt.SetFocus
    End Sub
    
    Private Sub CancelButton_Click()
    Unload Me
    End Sub
    
    
    Private Sub ClearButton_Click()
    Call Survey_Initialize
    End Sub
    
    
    Private Sub MultiPage1_Change()
    Me.MultiPage1.Value = 0
    End Sub
    
    
    Private Sub ClearButton1_Click()
    Dim ctl As MSForms.Control
    
        For Each ctl In Me.Controls
            Select Case TypeName(ctl)
                Case "TextBox"
                    ctl.Text = ""
                Case "CheckBox", "OptionButton", "ToggleButton"
                    ctl.Value = False
                Case "ComboBox", "ListBox"
                    ctl.ListIndex = -1
            End Select
        Next ctl
    End Sub
    
    
    Private Sub OKButton_Click()
    
    'error check - blank UserForm fields not permitted
    
    If FirstNametxt.Text = "" Then
    
    MsgBox "Please Enter First Name", vbOKOnly, "Name Error!"
    
    Exit Sub
    
    ElseIf LastNametxt.Text = "" Then
    
    MsgBox "Please Enter Last Name", vbOKOnly, "Name Error!"
    
    Exit Sub
    
    ElseIf Combobox1.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf Combobox2.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf combobox3.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf ComboBox4.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf ComboBox5.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf ComboBox6.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf ComboBox7.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf ComboBox8.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf Textbox1.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf Textbox2.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf textbox3.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf TextBox4.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    ElseIf TextBox5.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf TextBox6.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf TextBox7.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    ElseIf TextBox8.Value = "" Then
    
    MsgBox "Please Complete Form", vbOKOnly, "Country Error!"
    
    Exit Sub
    
    
    End If
    
    Dim emptyRow As Long
    
    'Make Sheet2 active
    Sheet2.Activate
    
    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
    
    'Transfer information
    Cells(emptyRow, 1).Value = FirstNametxt.Value
    Cells(emptyRow, 2).Value = LastNametxt.Value
    Cells(emptyRow, 3).Value = Combobox1.Value
    Cells(emptyRow, 4).Value = Textbox1.Value
    Cells(emptyRow, 5).Value = Combobox2.Value
    Cells(emptyRow, 6).Value = Textbox2.Value
    Cells(emptyRow, 7).Value = combobox3.Value
    Cells(emptyRow, 8).Value = textbox3.Value
    Cells(emptyRow, 9).Value = ComboBox4.Value
    Cells(emptyRow, 10).Value = TextBox4.Value
    Cells(emptyRow, 11).Value = ComboBox5.Value
    Cells(emptyRow, 12).Value = TextBox5.Value
    Cells(emptyRow, 13).Value = ComboBox6.Value
    Cells(emptyRow, 14).Value = TextBox6.Value
    Cells(emptyRow, 15).Value = ComboBox7.Value
    Cells(emptyRow, 16).Value = TextBox7.Value
    Cells(emptyRow, 17).Value = ComboBox8.Value
    Cells(emptyRow, 18).Value = TextBox8.Value
    
    
    
    'make sheet1 active
    Sheet1.Activate
    
    Unload Me
    End Sub

  2. #2
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    you have to use an if statement , like this :

    if Worksheets("Day 1 Answers").range("a1") = "My Text"   then ' change my text to the string that needs to exist in the specific cell , here its a1.
    ' what do you want to do
    'your code come here
    end if
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  3. #3
    Registered User
    Join Date
    01-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    83

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    Thanks xlbiznes! I think your part worked but I came up with an error code because of the same problem on my combobox. Can this be made conditional like the above??
    Combobox1.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)

  4. #4
    Registered User
    Join Date
    01-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    83

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    Thanks xlbiznes but I couldn't get that to work. Basically I don't want to hard code "My Text" because I want to be able to easily change it. So if there is some text in C1 then
    Question1.Caption = Worksheets("Day 1 Answers").Range("C1").Value
    but if not then don't put anything in there.

    Thanks,

  5. #5
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    try this

    if  Worksheets("Day 1 Answers").Range("C1") <> "" then
    Question1.Caption = Worksheets("Day 1 Answers").Range("C1").Value
    end if

  6. #6
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    try this

    If WorksheetFunction.CountA(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)  > 0 Then ' check if the range is empty
    Combobox1.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)
    end if

  7. #7
    Registered User
    Join Date
    01-05-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    83

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    clean survey.xlsm

    I couldn't get it to work. I'm probably missing something simple...

  8. #8
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    replace your Private Sub UserForm_Initialize() code with this and see what happens.

    Private Sub UserForm_Initialize()
    If Worksheets("Day 1 Answers").Range("C1") <> "" Then
    Question1.Caption = Worksheets("Day 1 Answers").Range("C1").Value
    End If
    Question2.Caption = Worksheets("Day 1 Answers").Range("E1").Value
    Question3.Caption = Worksheets("Day 1 Answers").Range("G1").Value
    Question4.Caption = Worksheets("Day 1 Answers").Range("I1").Value
    Question5.Caption = Worksheets("Day 1 Answers").Range("K1").Value
    Question6.Caption = Worksheets("Day 1 Answers").Range("M1").Value
    Question7.Caption = Worksheets("Day 1 Answers").Range("O1").Value
    Question8.Caption = Worksheets("Day 1 Answers").Range("Q1").Value
    
    If WorksheetFunction.CountA(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange) > 0 Then  ' check if the range is empty
    Combobox1.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)
    End If
    
    
    
    
    Combobox1.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_1").RefersToRange)
    Combobox2.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_2").RefersToRange)
    combobox3.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_3").RefersToRange)
    ComboBox4.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_4").RefersToRange)
    ComboBox5.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_5").RefersToRange)
    ComboBox6.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_6").RefersToRange)
    ComboBox7.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_7").RefersToRange)
    ComboBox8.List = WorksheetFunction.Transpose(ThisWorkbook.Names("Survey_1_Question_8").RefersToRange)
    
    
    
    End Sub

  9. #9
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,065

    Re: Need Help with VBA code that populates a dropdown in a userform conditionally

    Opened in Commercial Services subforum so I'll close this one.
    Never use Merged Cells in Excel

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 03-19-2014, 02:34 PM
  2. Quick and Dirty : User selects from dropdown (sht1)...macro populates data from (sht2)
    By Joecruz749 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2013, 05:06 PM
  3. Help a rookie make a dropdown that populates a sheet.
    By DareArkin in forum Excel Formulas & Functions
    Replies: 23
    Last Post: 03-01-2013, 01:47 AM
  4. [SOLVED] How can I code a Userform to provide dropdown boxes to filter data?
    By gozzard in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-13-2012, 05:28 PM
  5. Editing a code that sorts data for Dropdown in a UserForm
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-16-2011, 08:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1