Too many "Ifs and buts"

olorin

Board Regular
Joined
Feb 7, 2005
Messages
87
I have a list box on a userform that has a named range source.
The range has 100 text strings in it.
Next to the list box on the userform is a text box that I want the user to enter a number into.
I then want the textbox value to be put into a worksheet in a cell address based on the listbox selection without using 100 "IF...Else statements".
I know one or more of u Excel lent people know how to do this. I just ain't that hot on VBA.
Though I could do it with 100 IF else statements, it seems as though there must be an easier way.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How exactly is it determined where in the sheet the value is going to go?
 
Upvote 0
when the user selects an item from the named range in the list box, then enters a number in the text box, and clicks an "OK" button, I want the number to be placed in a cell relevant to the list box selection.
Thanx for your speedy reply
 
Upvote 0
OK, but can you give a sample of the choices and where they would go on the sheet? Does it need to go next to a description in the worksheeet? If so we could possibly search for that text in a range. Some more details would help.
 
Upvote 0
I want the result from the userinput to be placed into the last empty cell in a column based on the selection highlighted in the named range in the listbox.
The listbox is populated by a list of cells from a named range "products". All the products in the list have different names, if for example the user selects product "x" from the listbox and then enters "50" in the textbox and then clicks "OK", I want the Value "50" to be entered into the last empty cell in column "C". If they select product "y" from the listbox I want the Value "50" to be entered into the last empty cell in the column "E".
Hope this helps.
Thanx again
 
Upvote 0
Put the columns in the Array statement in the order that the corresponding values are in the listbox, and perhaps something like this:

Code:
Private Sub CommandButton1_Click()
Dim x, r As Range
x = Array("C", "E")
If ListBox1.ListIndex <> -1 Then
    Set r = Range(x(ListBox1.ListIndex) & "65536").End(xlUp)
    If r <> "" Then Set r = r.Offset(1, 0)
    r = TextBox1
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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