Userform problem (Listindex)

G

Gert-Jan

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
 
A

Ardus Petus

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
'----------------------------------------------
 
G

Gert-Jan

This is GREAT!! Thanks a lot!

Ardus Petus said:
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
'----------------------------------------------
 
D

Dave Peterson

You have another reply at your other post.

Gert-Jan said:
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
 

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

Top