View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.misc
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Excel vba UserForm Lists not registering default values?

Change the following procedures as shown...

Private Sub CommandButton1_Click()
Dim vDataOut(1 To 3, 1 To 1)
vDataOut(1, 1) = ListBox1.List(ListBox1.ListIndex)
vDataOut(2, 1) = ListBox2.List(ListBox2.ListIndex)
vDataOut(3, 1) = ListBox3.List(ListBox3.ListIndex)
Range("B16").Resize(UBound(vDataOut), 1) = vDataOut
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim sz$
With Me.ListBox1
.List = Array("a", "b", "c"): .ListIndex = 1
sz = sz & .List(.ListIndex) '//value
End With 'ListBox1

With Me.ListBox2
.List = Array("a", "b", "c"): .ListIndex = 2
sz = sz & .List(.ListIndex) '//value
End With 'ListBox2

With Me.ListBox3
.List = Array("a", "b", "c"): .ListIndex = 0
sz = sz & .List(.ListIndex) '//value
End With 'ListBox3
MsgBox sz

Call Descript(ListBox1.List(ListBox1.ListIndex), "Box 1")
Call Descript(ListBox2.List(ListBox2.ListIndex), "Box 2")
End Sub

Private Sub Descript(Selection As String, Target As String)
Dim sMsg$, sz$, rngLookup As Range
If Selection = "" Or Target = "" Then Exit Sub

Select Case Target
Case "Box 1"
sz = Application.VLookup(Selection, Sheets("Data").Range("A2:B4"),
2, False)
If sz = "" Then sMsg = "error reading value for 1": GoTo ErrExit
Descript1.Caption = sz

Case "Box 2"
sz = Application.VLookup(Selection, Sheets("Data").Range("D2:E4"),
2, False)
If sz = "" Then sMsg = "error reading value for 2": GoTo ErrExit
Descript2.Caption = sz
End Select 'Case Target
GoTo NormalExit

ErrExit:
MsgBox sMsg

NormalExit:
Set rngLookup = Nothing
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion