在Userform中创建下一个按钮以跳转到已过滤的Excel工作表中的下一行

时间:2016-05-30 15:41:58

标签: excel vba excel-vba

我创建了一个包含多个文本框的用户窗体,这些文本框中的值将被填入或显示在Excel工作表中找到的值。  enter image description here

我的问题是让Next和OK按钮起作用。当我按下确定时,它应该将所有数据转储到正确行上的Excel工作表中,然后使用“下一步”按钮移动到下一行。

问题是我不能简单地增加一个计数器,因为它应该跳转到活动过滤器的下一行,即45后的下一行可能是66.那么我该如何实现呢?

以下是我应用正确过滤器的代码:

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

我有一个名为nextLine的函数(rowNumber as long),它使用正确的值填充GUI。

2 个答案:

答案 0 :(得分:1)

你的代码看起来不错,除了我认为的一件事,你在init方法中做的事情:

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

Call Creo.updateGUI(Settings.nextIndex)

你将在GUI中显示表格的第三行而不是第一行过滤(我希望它是你想要的),因为columnheader已被设置为2并且然后你执行上面的代码nextindex = 3 columnheader + 1 )。
而是上面的代码可以做

Settings.NextIndex = columnHeader
call nextButton()

nextButton子查找过滤后的第一行,并在GUI中显示。我从来没有对代码进行过测试,这只是一个读你的想法。

埃托

答案 1 :(得分:0)

所以这是我的下一个按钮命令,但我并不完全像活动单元格方法(如果有人在使用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

这是我的init方法:

'*******************************************************************************************************
'****************** 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