如何检查过滤器是否有任何结果?

发布于 2025-02-09 17:57:33 字数 490 浏览 1 评论 0原文

我应用多个过滤器(在数组中定义),通过它们运行并导出PDF。

当过滤器没有值时,代码停止,因为没有任何导出。

我正在使用在错误简历下上使用,但这是不可持续的。如果错误发生两次,它将再次破裂。

如何检查过滤器是否有任何结果?

For i = 1 To 18

    FilterApply name:=FL(i)
    
    names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
    
    DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
        
Next

I apply multiple filters (defined in an array), run through them and export pdfs.

When a filter has no value the code stops because there is nothing to export.

I am using On Error Resume Next but this is not sustainable. If the error happens twice it breaks again.

How can I check if the filter has any results?

For i = 1 To 18

    FilterApply name:=FL(i)
    
    names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
    
    DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
        
Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2025-02-16 17:57:33

我找到了答案。实际上很容易!

    **SelectTaskField Row:=0, Column:="name"
    
    If ActiveCell.Text <> "" Then**

For i = 1 To 18

    FilterApply name:=FL(i)
    
    SelectTaskField Row:=0, Column:="name"
    
    If ActiveCell.Text <> "" Then

        If Left(FL(i), 1) = "_" Then ' create file name
            names = ActiveProject.ProjectSummaryTask.name & " " & Mid(FL(i), 2, Len(FL(i)))
        Else
            names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
        End If
        
        
        DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
        
    End If

Next

I found the answer. quite easy actually!

    **SelectTaskField Row:=0, Column:="name"
    
    If ActiveCell.Text <> "" Then**

For i = 1 To 18

    FilterApply name:=FL(i)
    
    SelectTaskField Row:=0, Column:="name"
    
    If ActiveCell.Text <> "" Then

        If Left(FL(i), 1) = "_" Then ' create file name
            names = ActiveProject.ProjectSummaryTask.name & " " & Mid(FL(i), 2, Len(FL(i)))
        Else
            names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
        End If
        
        
        DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
        
    End If

Next

北方的韩爷 2025-02-16 17:57:33

答案一个类似的问题以前在这里。我建议创建一个单独的函数以检查过滤器是否为空,

Public Function CurrentFilterHasTasks() As Boolean

   Dim result As Boolean
   On Error GoTo ErrHandler

   Application.SelectAll 'select everything in the current filter

   'Application.ActiveSelection.Tasks will fail if there are only blank rows in the active selection
   If Application.ActiveSelection.Tasks.Count > 0 Then
        result = True
   End If

   CurrentFilterHasTasks = result

   'call exit function here so the code below the error handler does not run
   Exit Function

ErrHandler:
   result = False
   CurrentFilterHasTasks = result
    
End Function

因此可以在代码中调用该方法:

For i = 1 To 18

    FilterApply name:=FL(i)
    
    If CurrentFilterHasTasks Then
       names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
    
       DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
    End If
        
Next

I've answered a similar question on here before. I recommend creating a separate function to check if the Filter is empty, like this:

Public Function CurrentFilterHasTasks() As Boolean

   Dim result As Boolean
   On Error GoTo ErrHandler

   Application.SelectAll 'select everything in the current filter

   'Application.ActiveSelection.Tasks will fail if there are only blank rows in the active selection
   If Application.ActiveSelection.Tasks.Count > 0 Then
        result = True
   End If

   CurrentFilterHasTasks = result

   'call exit function here so the code below the error handler does not run
   Exit Function

ErrHandler:
   result = False
   CurrentFilterHasTasks = result
    
End Function

Then you can call the method in your code:

For i = 1 To 18

    FilterApply name:=FL(i)
    
    If CurrentFilterHasTasks Then
       names = ActiveProject.ProjectSummaryTask.name & " " & FL(i)
    
       DocumentExport FileName:="C:\temp\" & names, FromDate:="01/07/22 6:00", ToDate:="15/07/22 18:00", FileType:=pjPDF
    End If
        
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文