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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,546
Messages
5,838,011
Members
430,526
Latest member
NiceGuyWithExcel2007

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
Top