Select the next item
Hi again Tendresse,
I understand now. Try the following for both Next and Previous . Note the
comments in the code re what you want to do when you get to the first or last
value.
Private Sub CommandButton1_Click()
'Sets list box to to next
Dim varTemp As Variant
varTemp = Sheets("Sheet1").ListBox1.ListIndex
'Note: ListBox Index starts at zero
If varTemp = Sheets("Sheet1").ListBox1.ListCount - 1 Then
MsgBox "At end of list"
'Exit Sub
'or the following to roll around to first value
Sheets("Sheet1").ListBox1.ListIndex = 0
Else
Sheets("Sheet1").ListBox1.ListIndex = varTemp + 1
End If
End Sub
Private Sub CommandButton2_Click()
'Sets list box to to previous
Dim varTemp As Variant
varTemp = Sheets("Sheet1").ListBox1.ListIndex
'Note: ListBox Index starts at zero
If varTemp = 0 Then
MsgBox "At start of list"
'Exit Sub
'or the following to roll around to last value
Sheets("Sheet1").ListBox1.ListIndex = _
Sheets("Sheet1").ListBox1.ListCount - 1
Else
Sheets("Sheet1").ListBox1.ListIndex = varTemp - 1
End If
End Sub
--
Regards,
OssieMac
"Tendresse" wrote:
Hi OssieMac,
Sorry if i wasn't very clear in my previous post.
The listbox in sheet1 contains some items, for example:
aaa
bbb
ccc
When the user selects one of them (say aaa), a click event takes place and a
userform is displayed containing information about item aaa. This userform
has a commandbutton with caption "View Next". When clicked, i want to display
a new userform that displays information about the next item which, in this
case, is bbb.
What i'm trying to do is to save the user the hassle of closing the current
userform, go back to sheet one and select a new item from the list. By having
"View Next" and "View Previous" in the userform itself, the user can flick
between the items without having to go back and forth to the original listbox.
So in order to populate the new userform with the new information about item
bbb, i need this item (bbb) to be selected so that it's reflected in the
Listbox's linkedCell.
I hope this makes more sense.
cheers
tendresse
"OssieMac" wrote:
Hi Tendresse,
Do you mean that if the user selects item 2 then you want item 2 to become
item 3 in the list like the following
Example Original list.
AAA
BBB
CCC
DDD
EEE
If the user selects BBB then you want the list to look like this
AAA
CCC
BBB
DDD
EEE
If my assumption is correct then need to know how the list is populated in
the list box. Is it a range in the worksheet or what?
--
Regards,
OssieMac
"Tendresse" wrote:
I have a listbox (created from the Control toolbar) in Sheet1. The list has a
number of items and the user can select only one item at a time. How can i
programmatically move the user's selection to the next item in the list?
So if the user originally selected item number 2 and it's highlighted in
blue, how can i move the selection to item number 3 using VBA?
Excel 2003
cheers, tendresse
|