Why won't this work?... UserForm code

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
This code works on all my existing UserForms
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
But now, with any new work, (on an existing worksheet or a new "test" worksheet) the UserForm will not display the called value in TextBox1
<o:p></o:p>
Any idea what’s wrong?
<o:p></o:p>
Code:
Private Sub UserForm_Initialize()<o:p></o:p>
Dim x&<o:p></o:p>
With Sheets("Sheet1")<o:p></o:p>
    For x = 2 To .Cells(Rows.Count, "A").End(xlUp).Row<o:p></o:p>
        ComboBox1.AddItem (.Range("A" & x).Value)<o:p></o:p>
    Next x<o:p></o:p>
End With<o:p></o:p>
End Sub
<o:p></o:p>
<o:p></o:p>
<o:p>
Code:
 </o:p>
Private Sub ComboBox1_Change()<o:p></o:p>
Dim x&<o:p></o:p>
With Sheets("Sheet1")<o:p></o:p>
    For x = 1 To .Cells(Rows.Count, "A").End(xlUp).Row<o:p></o:p>
        If .Cells(x, "A").Value = Me.ComboBox1.Value Then _<o:p></o:p>
            Me.TextBox1.Value = .Cells(x, "B").Value<o:p></o:p>
<o:p></o:p>
    Next x<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
End Sub

Very puzzled!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
OK!!! I figured it out!

TextBox1 value will remain blank if ColumnA values are numeric.

Since my WS ColumnA is the date, how can I overcome this?
 
Upvote 0
You could use the ListIndex property of the combobox to get the row for the selected item.
For example if the combobox is populated from A2:A10 the index of the first item is 0. So to get the row just add 2.

Textbox1.Value = Range("B" & Combobox1.ListIndex + 2)
 
Upvote 0
OK...Thanks!

And this provides math functions and currency format for the result.

Code:
Private Sub ComboBox1_Change()
TextBox1.Value = (Format(Range("T" & ComboBox1.ListIndex + 2) + Range("U" & ComboBox1.ListIndex + 2) * 0.25, "$ #.00"))
TextBox5.Value = (Format(Range("AE" & ComboBox1.ListIndex + 2), "$ #.00"))

So why does my first bit not work when ColumnA is numeric? Seems odd.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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