View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default List box value returns the row number of the list box range row

There are two listboxes you can place on a worksheet.

The first is from the Forms toolbar. Its value is an index into its list.

The second is from the Control toolbox toolbar. Its value is the value you see
in the listbox.

If your listbox (either type) is a single selection, then you don't need to use
a macro to get the value.

If the listbox is from the forms toolbar:
rightclick on the listbox
Choose Format Control
Then on the Control tab, you can assign a cell link (say A1).

Then in B1 (the second cell), you can use that value/index in A1 to retrieve the
value from the list:

=if(a1=0,"",index(sheet2!a1:a11,a1)
where your list is on sheet2 in cells A1:A11


If your listbox is from the control toolbox toolbar, you can just go into design
mode (another icon on that toolbar) and then

rightclick on the listbox
Choose Properties
Scroll down to LinkedCell and type in the address of the cell you want to use.


If your listbox (either type) is multiselect, then this won't work and you will
need code.

=========
It sounds like you're using the listbox from the Forms toolbar.

If you have to use code (as part of a bigger routine), then you could use:

dim myLB8Val as string
with worksheets("timer")
with .listboxes("list box 8")
if .listindex = 0 then
mylb8val = "" 'not chosen
else
mylb8val = .list(.listindex)
end if
end with
end with

msgbox mylb8val

Tim at MBS wrote:

Hi, I was hoping someone could help me figure out what I am doing wrong.

I have a list box set up on a worksheet and I want the user to select a
value from the list box, and then have that value entered into a cell when a
button is clicked.

When I run/step through the macro, it takes a value and places it into the
appropriate cell. The problem is that the value which is placed in the cell
is not the value from the listbox, but the row number of the value that was
chosen. Using the below row numbers and values as an example; if I select
value 7 from the list box, the value that is returned is 2, not 7. If I
select 9A, the value returned is 4. The listbox value range is on different
worksheet than the worksheet where I want to place the listbox value. The
listbox range is currently one column with about 200 rows.

Worksheet Row Number Value
1 6
2 7
3 8
4 9A
5 10B

The condensed version of the code (excluding how I choose the cell) is:

Dim listvalue As String
Dim rowcounter As Integer
Dim EOBpointer As Range

Worksheets("Timer").Activate
ActiveSheet.Shapes("List Box 8").Select

rowcounter = 35

listvalue = Selection.Value

Set EOBpointer = Range("c" & rowcounter)
EOBpointer.Select
Selection.Value = listvalue


--

Dave Peterson