Selecting data in ListBox

D

donwb

I have a 2 column ListBox containing 10 rows of data.
When the user clicks a row,
I want to allocate the data in the first column to one variable
and the data in the second column to a second variable.
I can get the column 1 data using :-
Variable1=UserForm1.ListBox1.Text
but how do I get the column 2 data?
donwb
 
D

Dave Peterson

Maybe something like:

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim Col1Var As String
Dim Col2Var As String

With Me.ListBox1
If .ListIndex < 0 Then
'nothing selected, do nothing
Else
Col1Var = .List(.ListIndex, 0)
Col2Var = .List(.ListIndex, 1)
MsgBox Col1Var & vbLf & Col2Var
End If
End With
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.MultiSelect = fmMultiSelectSingle
.ColumnCount = 2
.List = Worksheets("sheet1").Range("a1:B10").Value
End With
End Sub
 
D

donwb

Many thanks Dave.
What was throwing me was the old problem of
column 1 is actually column 0
donwb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Mail Merge Help 1
Need Help with a VBA subroutine 0
Problem with ListBox, List Index 2
Listbox problem 13
ListBox Item Row Number 1
Dates In Excel 5
How to format ListBox columns 3
Variable Range, selecting/copying 1

Top