2

我需要帮助检查不包括标题的自动过滤行。我希望它给出一个消息框“未找到记录”。如果标题行之外有行,则退出 sub 或继续复制粘贴。我知道在过滤器之后我需要一个 If/Else 条目来检查数据,但我无法确定如何检查。此代码是从我创建的用户窗体按钮完成的。

这是我的脚本:

Private Sub Searchbycompanyfield_Click()

If CompanyComboBox1.Value = "" Then
    MsgBox "Please enter a Company to begin search."
    Exit Sub
End If
ActiveSheet.Range("$A:$H").AutoFilter Field:=1, Criteria1:=EQDataEntry.CompanyComboBox1.Value, Operator:=xlOr
Cells.Select
Selection.Copy
Sheets("Sheet2").Select
Range("A5").Select
ActiveSheet.Paste
Call MessageBoxYesOrNoMsgBox
End Sub

任何帮助将不胜感激。

4

5 回答 5

3

见下文,SpecialCells(xlCellTypeVisible) 将允许您返回过滤单元格的 rng 对象。您只需要根据您的情况检查其中的行数:

Private Sub Searchbycompanyfield_Click()

    If CompanyComboBox1.Value = "" Then
        MsgBox "Please enter a Company to begin search."
    Exit Sub
    End If

    Dim sh As Worksheet
    Dim rng As Range

    Set sh = ActiveSheet

    sh.AutoFilterMode = False
    sh.Range("$A:$H").AutoFilter Field:=1, Criteria1:=EQDataEntry.CompanyComboBox1.Value, Operator:=xlOr

    Set rng = sh.UsedRange.SpecialCells(xlCellTypeVisible)

    If (rng.Rows.Count > 1) Then

        rng.Copy Sheets("Sheet2").[A5]

        Call MessageBoxYesOrNoMsgBox

    End If

End Sub
于 2012-10-02T12:08:47.750 回答
3

计算行数,或检查最后一行是否为标题

if application.worksheetfunction.subtotal(3,activesheet.columns(1))>1 then 
    msgbox "Records"
else
    msgbox "No Records"
end if

检查最后一行

if activesheet.cells(rows.count,1).end(xlup).row>1 then 
    msgbox "Records"
else
    msgbox "No Records"
end if
于 2012-10-01T22:12:52.327 回答
0

对于其他需要这个的人,我最终使用:

Private Sub Searchbycompanyfield_Click()

If CompanyComboBox1.Value = "" Then
    MsgBox "Please enter a Company to begin search."
Exit Sub
End If

Dim sh As Worksheet
Dim rng As Range

Set sh = ActiveSheet

sh.AutoFilterMode = False
sh.Range("$A:$H").AutoFilter Field:=1, Criteria1:=EQDataEntry.CompanyComboBox1.Value, Operator:=xlOr

Set rng = sh.UsedRange.SpecialCells(xlCellTypeVisible)

If (rng.Rows.Count > 1) Then

    rng.Copy Sheets("Sheet2").[A5]
    Sheets("Sheet2").Select
    Call MessageBoxYesOrNoMsgBox

Else
If ActiveSheet.AutoFilterMode Then ActiveSheet.Cells.AutoFilter
MsgBox "No records found."
Exit Sub
End If

End Sub

再次感谢你们的帮助。

于 2012-10-02T16:46:36.030 回答
0

这是您重构的maco,以演示使用过滤器范围的方法。也消除了Select范围的需要

Sub Searchbycompanyfield()

    If CompanyComboBox1.Value = "" Then
        MsgBox "Please enter a Company to begin search."
        Exit Sub
    End If

    Dim sh As Worksheet
    Dim rng As Range

    Set sh = ActiveSheet
    ' clear any existing autofilter
    sh.AutoFilterMode = False
    sh.Range("$A:$H").AutoFilter Field:=1, _
        Criteria1:=EQDataEntry.CompanyComboBox1.Value, Operator:=xlOr

    Set rng = sh.AutoFilter.Range
    ' Check if there is any data in filter range
    If rng.Rows.Count > 1 Then
        Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
        On Error Resume Next
        Set rng = rng.SpecialCells(xlCellTypeVisible)
        If Err.Number = 1004 Then
            ' No cells returned by filter
            Exit Sub
        End If
        On Error GoTo 0
        rng.Copy ActiveWorkbook.Worksheets("Sheet2").[A5]

    End If
    ' remove filter
    sh.AutoFilterMode = False
    MessageBoxYesOrNoMsgBox

End Sub
于 2012-10-02T06:50:36.203 回答
0

我找到了解决方案。试试这个解决方案。

Dim count As Long
count = Application.WorksheetFunction.count(rng_SmPrt.SpecialCells(xlCellTypeVisible))

这个正确返回可见行的数量。

于 2018-12-04T06:07:40.603 回答