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.
 

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
How exactly is it determined where in the sheet the value is going to go?
 

olorin

Board Regular
Joined
Feb 7, 2005
Messages
87
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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

olorin

Board Regular
Joined
Feb 7, 2005
Messages
87

ADVERTISEMENT

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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,118,110
Messages
5,570,250
Members
412,313
Latest member
pauloalex
Top