How can I select the next item and next item and next item?

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
508
Office Version
365
Platform
Windows
Private Sub CommandButton1_Click()
For I = 0 To Me.ListBox4.ListCount - 1
If InStr(UCase(ListBox4.List(I, 1)), UCase(sFind)) > 0 Then
Me.ListBox4.TopIndex = I + 1
Me.ListBox4.ListIndex = I + 1
Exit For
End If
Next I
End Sub

How can I replace the "I + 1" with a variable to add 1 after every click?
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,924
Office Version
2010
Platform
Windows
What exactly is your code supposed to be doing?
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,849
Office Version
365
Platform
Windows
Try
Code:
Private Sub CommandButton1_Click()
Static k As Long
sFind = Me.TextBox4.Value
For i = k + 1 To Me.Listbox4.ListCount - 1
If InStr(UCase(Listbox4.List(i, 1)), UCase(sFind)) > 0 Then
   Me.Listbox4.TopIndex = i
   Me.Listbox4.ListIndex = i
   k = i
   Exit For
End If
If i = Me.Listbox4.ListCount - 1 Then k = 0
Next i
End Sub
 

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
508
Office Version
365
Platform
Windows
Rick,

After It searches what I typed in a textbox, I want this command button to find the next line that matches, and then the next line and next line.
 

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
508
Office Version
365
Platform
Windows
Fluff,

Thanks again! This selects the next items on the list. I just need to add a better filter.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,849
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,099,363
Messages
5,468,186
Members
406,570
Latest member
Ktvu2006

This Week's Hot Topics

Top