View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ardus Petus Ardus Petus is offline
external usenet poster
 
Posts: 718
Default Userform problem (Listindex)

Listindex starts with 0

See amended code:

HTH
--
AP

'-----------
Private Sub ComboBox1_Change()
Dim i As Long 'Index
i = ComboBox1.ListIndex + 1
Label1.Caption = Cells(i, 2).Value
Label2.Caption = Cells(i, 3).Value
Label3.Caption = Cells(i, 4).Value
CommandButton1.Enabled = i < ComboBox1.ListCount
CommandButton2.Enabled = i 1
End Sub

Private Sub CommandButton1_Click() 'Next Button
ComboBox1.ListIndex = ComboBox1.ListIndex + 1
Call ComboBox1_Change
End Sub

Private Sub CommandButton2_Click() 'Previous Button
ComboBox1.ListIndex = ComboBox1.ListIndex - 1
Call ComboBox1_Change
End Sub
'----------------------------------------------

"Gert-Jan" a écrit dans le message de
...
In A1 - D20 I have values. The range A1 - A20 is the input for the

ComboBox
in my userform. What should happen is the following: ComboBox - onchange:
refresh the data in the labels. If the selected item in the ComboBox is

the
value from A10, in Label 1 should the value of B10 appear, etc. And: if

the
first item is selected, the PREVIOUS button must be disabled, when the

last
item is selected, the NEXT button.

I have some errors in the code:

- when the first item is selected, I get an error
- when the second item is selected, the button NEXT is disabled
- when the last item is selected, the button NEXT is still enabled,

pressing
it will lead to an error

I think there are some errors in the lines "i = ComboBox1.ListIndex". I

made
some changes, but they didn't work properly. Any suggestions??

--------

Private Sub ComboBox1_Change()
Dim i As Long 'Index
i = ComboBox1.ListIndex
If i = ComboBox1.ListCount Or i = 1 Then CommandButton1.Enabled =

False
Label1.Caption = Cells(i, 2).Value
Label2.Caption = Cells(i, 3).Value
Label3.Caption = Cells(i, 4).Value
End Sub

Private Sub CommandButton1_Click() 'Next Button
Dim i As Long
i = ComboBox1.ListIndex
If i < ComboBox1.ListCount Then
ComboBox1.ListIndex = i + 1
If i + 1 = ComboBox1.ListCount Then CommandButton1.Enabled = False
Else
CommandButton1.Enabled = False
End If
End Sub

Private Sub CommandButton2_Click() 'Previous Button
Dim i As Long
i = ComboBox1.ListIndex
If i 1 Then
ComboBox1.ListIndex = i - 1
If i - 1 = 1 Then CommandButton2.Enabled = False
Else
CommandButton2.Enabled = False
End If
End Sub