简体   繁体   English

在用户窗体中创建一个下一步按钮以跳转到过滤后的 Excel 工作表中的下一行

[英]Make a next button in a Userform to jump to next row in a filtered excel sheet

I made a userform with several textboxes, the values in these textboxes are to be filled in or to show values found in an excel sheet.我制作了一个带有多个文本框的用户表单,这些文本框中的值将被填充或显示在 Excel 工作表中找到的值。 在此处输入图片说明

My problem is to get the Next and OK button to work.我的问题是让 Next 和 OK 按钮起作用。 When I press Ok, it should dump all the data to the excel sheet on correct row, and with Next button it should move to next row.当我按 Ok 时,它应该将所有数据转储到正确行的 Excel 表中,并使用 Next 按钮移动到下一行。

The problem is that I can not simply increase a counter because it should jump to next row in the active filter, ie next row after 45 might be 66. So how can I achieve this?问题是我不能简单地增加一个计数器,因为它应该跳到有源过滤器的下一行,即 45 之后的下一行可能是 66。那么我该如何实现呢?

Below is my code for applying the correct filter:下面是我应用正确过滤器的代码:

Dim columnHeader As Integer
Dim ws As Worksheet
Dim Wb As Workbook
Dim lo As ListObject
Dim lsColumn As Integer
Dim counter As Integer
Dim rRange As Range, filRange As Range, Rng As Range

'set the worksheet we will be dealing with
Set Wb = ActiveWorkbook
Set ws = Wb.Sheets("List")
Call Filter.Unhide_All_Columns ' just clear all filters

'Filter our sheet
columnHeader = 2
lsColumn = HelpFunctions.getColumn("LS", ws, columnHeader, True) 'Find correct colum, i.e 18 in this case
Set lo = ws.ListObjects("FilterParts")
lo.Range.AutoFilter Field:=lsColumn, Criteria1:=""
lo.Range.Cells.ClearFormats
lo.AutoFilter.ApplyFilter

'~~> Filter, offset(to exclude headers)
Settings.filRange = lo.Range.Offset(columnHeader, 0).SpecialCells(xlCellTypeVisible).EntireRow ' Assign it to a global variable so it can be reused

I have a function called nextLine(rowNumber as long) which populates the GUI with right values.我有一个名为 nextLine(rowNumber as long) 的函数,它用正确的值填充 GUI。

Your code looks good except for one thing in my opinion, in the init method when you do:您的代码看起来不错,除了我认为的一件事,在您执行以下操作时的 init 方法中:

Settings.nextIndex = columnHeader + 1
ws.Cells(Settings.nextIndex, 1).Select

Call Creo.updateGUI(Settings.nextIndex)

you will show in the GUI the third line of the table instead of the first line filtered (that i expect it's what you want) because columnheader is been set to 2 and then you do the code above that will gave to you nextindex = 3 ( columnheader + 1 ).您将在 GUI 中显示表格的第三行而不是过滤后的第一行(我希望这是您想要的),因为columnheader被设置为2 ,然后您执行上面的代码,该代码将为您提供nextindex = 3列标题 + 1 )。
Instead the code above you can do相反,您可以执行上面的代码

Settings.NextIndex = columnHeader
call nextButton()

and let the nextButton sub to find the first row filtered and show it in the GUI.并让nextButton子查找过滤后的第一行并将其显示在 GUI 中。 I've never tested the code, it's just an idea reading yours.我从来没有测试过代码,这只是阅读你的一个想法。

Ettore埃托雷

So here is my next button command, I do not completly like the active cell method though(might fail if someone click in the sheet while using the GUI.所以这是我的下一个按钮命令,虽然我不完全喜欢活动单元格方法(如果有人在使用 GUI 时单击工作表可能会失败。

'*********************************************************************************************************
'****************** This one gets triggered when the next button event occurs ****************************
'*********************************************************************************************************
Public Sub nextButton()
    Dim i As Long
    For i = Settings.nextIndex To Settings.filRange.Row
        ActiveCell.Offset(1, 0).Select
        If Not ActiveCell.EntireRow.Hidden Then
            Exit For
        End If
    Next i
    Settings.nextIndex = ActiveCell.Row
    Call Creo.updateGUI(Settings.nextIndex)


    'Reached last row
    If Settings.nextIndex = Settings.filRange.Row Then
        'Remove any filters
        Call Filter.Unhide_All_Columns
        'Hide GUI
        MOM.Hide
    End If

End Sub

Here is my init method:这是我的初始化方法:

'*******************************************************************************************************
'****************** THIS ONE GETS CALLED FROM EXCEL ****************************************************
'*******************************************************************************************************

Sub addNewLs()
    Dim columnHeader As Integer
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim lo As ListObject
    Dim lsColumn As Integer
    Dim counter As Integer
    Dim rRange As Range, filRange As Range, Rng As Range

    'set the worksheet we will be dealing with
    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("List")
    Call Filter.Unhide_All_Columns ' just clear all filters

    'Filter our sheet
    columnHeader = 2
    lsColumn = HelpFunctions.getColumn("LS", ws, columnHeader, True) 'Find correct colum, i.e 18 in this case
    Set lo = ws.ListObjects("FilterParts")
    lo.Range.AutoFilter Field:=lsColumn, Criteria1:=""
    lo.Range.Cells.ClearFormats
    lo.AutoFilter.ApplyFilter

    Set Settings.filRange = lo.Range.Cells(lo.Range.Rows.Count, lo.Range.Columns.Count)

    'lo.Range.Offset(columnHeader, 0).SpecialCells(xlCellTypeVisible).EntireRow ' Assign it to a global variable so it can be reused

    Settings.nextIndex = columnHeader + 1
    ws.Cells(Settings.nextIndex, 1).Select

    Call Creo.updateGUI(Settings.nextIndex)
    Call MOMModule.initiliazeMOM
End Sub

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

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