简体   繁体   English

下一步按钮用户窗体Excel VBA

[英]Next buttons userform Excel VBA

I have an admin sheet that has a column containing a list of True and False. 我有一个管理表,其中包含一列包含True和False的列表。 I am building a userform UI so users can click next (for now - building previous button after making next work), the userform will show the next False item in admin sheet and its corresponding data in Sheet1 will be displayed in Textbox1. 我正在构建一个用户窗体UI,因此用户可以单击“下一步”(现在-在进行下一个工作后构建上一个按钮),该用户窗体将在管理表中显示下一个False项目,并且在Sheet1中其对应的数据将显示在Textbox1中。

Reason for this is the row id in admin sheet correlates with Sheet1. 原因是管理工作表中的行ID与Sheet1相关。 So if data in Sheet1 row(31) has something wrong, column(13) in Admin sheet row(31) will be False. 因此,如果Sheet1行(31)中的数据有问题,则管理工作表行(31)中的列(13)将为False。

Code: 码:

Dim n As Long
Private Sub CommandButton1_Click()
Dim LR As Long
LR = Sheets("Sheet1").Cells(Rows.count, "B").End(xlUp).row

n = 7
With Worksheets("Admin")
    For i = n To LR
        If .Cells(i, 13).Value = "False" Then
            With Worksheets("Sheet1")
                Me.TextBox1 = .Cells(i, 2).Value
                Exit For
            End With
        End If
    Next i
End With
n = i + 1

End Sub

This successfully goes to the next False item and displays it correctly in Textbox1. 这将成功转到下一个False项目,并在Textbox1中正确显示它。 However, it does not iterate to the next one.. 但是,它不会迭代到下一个。

Whatever logic we use to set up Next, I am going to assume Previous will be the same? 无论我们用来设置Next的逻辑是什么,我都将假定Previous将是相同的?

Thanks guys. 多谢你们。

You can do something like this: 您可以执行以下操作:

Sub cmdNext_Click()
    FindRow True
End Sub

Sub cmdPrev_Click()
    FindRow False
End Sub


Private Sub FindRow(bForward As Boolean)
    Const RW_START As Long = 7

    Dim LR As Long, t As Long, dir As Long, i As Long
    LR = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row

    'going forwards, or back?
    If bForward Then
        n = IIf(n = 0, RW_START, n + 1) '<< Start at top
        t = LR                          '<< towards here
        dir = 1                         '<< increasing
    Else
        n = IIf(n = 0, LR, n - 1)       '<< Start at bottom
        t = RW_START                    '<< towards here
        dir = -1                        '<< decreasing
    End If

    For i = n To t Step dir
        If Worksheets("Admin").Cells(i, 13).Value = "False" Then
            Me.TextBox1 = Worksheets("Sheet1").Cells(i, 2).Value
            n = i
            Exit For
        End If
    Next i

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM