A NEXT and PREVIOUS command button to cycle through a list

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I've created a UserForm that is intended to operate similarly to what happens when you press Ctrl+F, as far as it needs to have the functionality to display 1 row's worth of items from a list that matches the criteria of the IF function. It should start at the first row that matches the criteria, and then cycle down the list by press the the NEXT button (Me.CommandButton1), or back up by pressing the PREVIOUS button (Me.CommandButton2). I want it to stop at the bottom of the list, instead of cycling back through, although I wouldn't mind having a new button appear on the last item that says "START FROM BEGINNING". Also, I'd really like to have a way to display which entry is currently displayed, in a "# of total#" format, just like when you press Ctrl+F. I tried this myself with the following code, and realized quickly after running it that it just immediately displays the last entry (duh), and I couldn't figure out how to use a command button click to control the "Next c" portion, and I have absolutely no clue how to make it go backwards. Thanks!


Code:
Private Sub UserfForm_Initialize()

Dim ws1     As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
Dim c     As Range
Dim Lr     As Long

Lr = ws1.Range("A" & Rows.Count).End(xlUp).Row

For Each c in ws1.Range("T7:T & Lr)
     If c.Value = "" Or c.Value = "INACTIVE P" & UserForm10.ComboBox1.Value Then
          Me.TextBox2.Value = c.Offset(0, -18).Value
          Me.TextBox3.Value = c.Offset(0, -17).Value
          Me.TextBox8.Value = c.Offset(0, -12 + UserForm10.ComboBox1.Value).Value
     End If
Next c

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This is a simple one I created that searches each row starting at 1 in column A for what ever is typed in the textbox. When it finds a match it reports the row in one label and the value in the next.

In Module

Code:
Option Explicit

Public LastRow As Long
Public CurrentRow As Integer


Sub PrimeList()
LastRow = ActiveSheet.Range("A65536").End(xlUp).Row
CurrentRow = 0
End Sub

Button on worksheet
Code:
Option Explicit


Private Sub CommandButton1_Click()
PrimeList
UserForm1.LabelCurrent.Caption = "Row - "
UserForm1.LabelValue.Caption = ""
UserForm1.Show
End Sub

User form (UserForm1) code behind Button called "CButtonNext". User form has a textbox called "TextBox1" and 2 labels called "LabelCurrent" and "LabelValue".
Code:
Option Explicit

Private Sub CButtonNext_Click()
Dim ValList As Long
CurrentRow = CurrentRow + 1
If CurrentRow > LastRow Then
    MsgBox "Last row reached", vbOKOnly, ThisWorkbook.Name
Else
    For ValList = CurrentRow To LastRow
        If InStr(1, ActiveSheet.Range("A" & ValList).Value, Me.TextBox1.Value) > 0 Then
            CurrentRow = ValList
            Me.LabelCurrent.Caption = "Row - " & CurrentRow
            Me.LabelValue.Caption = ActiveSheet.Range("A" & ValList).Value
            Exit For
        End If
    Next ValList
End If
End Sub

Hopefully give you some ideas
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top