ComboBox value IF...

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
554
I’m using this code to populate my UserForm ComboBoxes<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Rich (BB code):
Private Sub UserForm_Initialize()
Label1.Caption = "Position " & Sheet1.Range("AB2").Value<o:p></o:p>
Label2.Caption = "Position " & Sheet1.Range("AB3").Value<o:p></o:p>
Label3.Caption = "Position " & Sheet1.Range("AB4").Value<o:p></o:p>
Label4.Caption = "Position " & Sheet1.Range("AB5").Value<o:p></o:p>
<o:p></o:p>
Dim x&<o:p></o:p>
With Sheet1<o:p></o:p>
For x = 1 To .Cells(Rows.Count, "D").End(xlUp).Row<o:p></o:p>
 ComboBox1.AddItem (.Range("D" & x).Value)<o:p></o:p>
 ComboBox2.AddItem (.Range("D" & x).Value)<o:p></o:p>
 ComboBox3.AddItem (.Range("D" & x).Value)<o:p></o:p>
 ComboBox4.AddItem (.Range("D" & x).Value)<o:p></o:p>
 Next x<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
End Sub
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
And, this code to fill the cell values from the ComboBoxes<o:p></o:p>
<o:p></o:p>
Rich (BB code):
Private Sub CommandButton1_Click()
With Sheet1<o:p></o:p>
 .Range("AC2").Value = ComboBox1.Value<o:p></o:p>
 .Range("AC3").Value = ComboBox2.Value<o:p></o:p>
 .Range("AC4").Value = ComboBox3.Value<o:p></o:p>
 .Range("AC5").Value = ComboBox4.Value<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
Unload Me<o:p></o:p>
<o:p></o:p>
End Sub
<o:p></o:p>
<o:p></o:p>
So…<o:p></o:p>
How does the ComboBox.Value code change if AC2:AC5 values pre-exist?<o:p></o:p>
<o:p></o:p>
In other words<o:p></o:p>
IF AC2 is blank, Run Sub<o:p></o:p>
IF AC2.Value = “any value”, Then ComboBox1.Value = AC2.Value

Thanks for helping.

PS: Cleaner code for these two subs is appreciated<o:p></o:p>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Not sure what you are asking. This fills the comboboxes with any "pre-existing" values from AC2:AC5

Code:
Private Sub UserForm_Initialize()

    Dim x&, rng As Range
    
    With Sheet1

        Label1.Caption = "Position " & .Range("AB2").Value
        Label2.Caption = "Position " & .Range("AB3").Value
        Label3.Caption = "Position " & .Range("AB4").Value
        Label4.Caption = "Position " & .Range("AB5").Value
        
[COLOR="Red"]        ComboBox1.Value = .Range("AC2").Value
        ComboBox2.Value = .Range("AC3").Value
        ComboBox3.Value = .Range("AC4").Value
        ComboBox4.Value = .Range("AC5").Value[/COLOR]
        
        Set rng = .Range("D1", .Cells(Rows.Count, "D").End(xlUp))
        ComboBox1.RowSource = "'" & .Name & "'!" & rng.Address
        ComboBox2.RowSource = "'" & .Name & "'!" & rng.Address
        ComboBox3.RowSource = "'" & .Name & "'!" & rng.Address
        ComboBox4.RowSource = "'" & .Name & "'!" & rng.Address
            
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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