【问题标题】:Macro to iterate through a field filter one at a time using VBA Microsoft project 2010使用 VBA Microsoft project 2010 一次遍历一个字段过滤器的宏
【发布时间】:2015-08-19 08:53:55
【问题描述】:

基本上,正如标题所暗示的那样,我正在尝试遍历一个名为“Text3”的自定义字段,该字段与“任务所有者”相关联,一次下拉过滤一个资源。我是新手,所以请多多包涵。

这是我的代码:


Sub Macro1()
' Macro Macro1
' Macro Recorded Fri 5/29/15 by Valencia, Jonathan.
' suppose to go down the task owner filtered list for each resource and
' print to xps

    Dim res As resource, name As String

    'apply the gantt view first
    ViewApply name:="gantt chart"
    'expand all tasks
    OutlineShowAllTasks
    'apply the late task filter
    FilterApply name:="Late Tasks"

For Each res In ActiveProject.Resources
    name = res.name


    'apply filter to task owner as the resource
    'checks to see if filter is set on the application first
    If Not ActiveProject.AutoFilter Then
        Application.AutoFilter
    End If

    Application.SetAutoFilter FieldName:="Text3", _
                FilterType:=pjAutoFilterCustom, _
                Test1:="contains", Criteria1:=name


    'export to xps with the resources' name
    DocumentExport FileName:=name, FileType:=pjXPS



Next res


End Sub
*************************************************************************

我遇到的问题是它没有为该特定资源设置过滤器,而只是将其留空。如果我给出标准,例如“john smith”,它会起作用,但我正在尝试使用变量名称作为字符串来遍历所有资源。

任何帮助将不胜感激。 谢谢。

【问题讨论】:

  • 您确定 Text3 中的值与您的资源名称完全匹配吗?我试过你的代码,对我来说效果很好。例如,我创建了一个名为“Rachel”的资源,并将该值放在 Text3 字段中用于一些后期任务,并且过滤器运行良好。
  • 我刚查了一下,你说得对,名字不完全匹配......
  • 那么我将如何遍历该过滤列表?是否有一个数组包含为该过滤列表存储的所有值?

标签: vba ms-office ms-project


【解决方案1】:

根据您的 cmets,此代码将构建一个存储在 Text3 字段中的唯一名称列表,然后遍历它们以创建 XPS 报告。

Sub CreateXpsReports()

    ' build unique list of names store in Text3
    On Error Resume Next
    Dim Names As New Collection
    Dim tsk As Task
    For Each tsk In ActiveProject.Tasks
        Names.Add tsk.Text3, tsk.Text3
    Next tsk

    On Error GoTo 0

    'apply the gantt view first
    ViewApply name:="gantt chart"
    'expand all tasks
    OutlineShowAllTasks
    'apply the late task filter
    FilterApply name:="Late Tasks"

    Dim name As Variant
    For Each name In Names
        'This was the only line I was missing for the code to work.
        On Error Resume Next           

        'apply filter to task owner as the resource
        'checks to see if filter is set on the application first
        If Not ActiveProject.AutoFilter Then
            Application.AutoFilter
        End If

        Application.SetAutoFilter FieldName:="Text3", _
                    FilterType:=pjAutoFilterCustom, _
                    Test1:="contains", Criteria1:=name

        'export to xps with the resources' name
        DocumentExport FileName:=name, FileType:=pjXPS

    Next name

End Sub

【讨论】:

  • 感谢您的努力,但过滤器仍然是空的。会不会是我的 Text3 的布局?
  • 我不确定您所说的“布局”是什么意思。您是否尝试过使用 F8 单步执行代码?确保名称符合您的预期。
  • 是的,我试过了,还是没用。如果它没有挂在过滤器上,它就会挂在 XPS 部分。感谢您的所有帮助!
  • @JonathanValencia 注意,我省略了On Error Resume Next 并使用On Error GoTo 0 来匹配您没有错误处理的原始代码。但是,很高兴听到代码现在可以通过您的编辑为您工作。 (FWIW:您可以删除 On Error GoTo 0 和后续的On Error Resume Next 以获得相同的结果。)
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 2015-08-24
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多