首页 > 解决方案 > 如何根据文本输入将标志字段设置为是或否 - Microsoft-Project-VBA

问题描述

我正在尝试根据文本是否在任务描述中自动创建标志字段。目标是使用文本搜索项目文件,并在带有文本的每一行中,在标志字段中将“是”放在它旁边。

我能够使用行 ID 生成 MS 项目中的活动列表。

我不确定如何使用此列表在 Flag 字段中生成“yes No”

Sub Findtask()
Dim sTask As Task 'Summary level Task'
Dim aTask As Task 'Job level Task'
Dim Proj As Project 

x = InputBox$("Search for tasks that include the following text in their names:") 
Set Proj = ActiveProject
'Search for tasks tat include the following text in their names:"'
If Not x = "" Then
    For Each aTask In Proj.Tasks
    If InStr(1, aTask.Name, x, 1) Then
        y = y & vbCrLf & aTask.ID & ": " & aTask.Name
    End If
    Next aTask
    ' If No tasks exist then end'
    If Len(y) = 0 Then
        MsgBox "No Tasks with the text" & x & " found in the project", vbExclamation
        Else
            MsgBox y
        End If
    End If
End Sub

请参阅下面的图片

在此处输入图像描述

在此处输入图像描述

Example of this

ID    Task Name  Flag 1(Hydro)
1     Hydro 1    Yes
2     basket 1   No
3     Hydro 2   Yes

标签: vbams-project

解决方案


此代码将设置一个标志字段(在本例中Flag1)。如果任务名称字段包含所需的文本,则标志将设置为是,否则为否。

Sub FlagTasks()

    Dim txt As String
    txt = InputBox("Flag tasks that include the following text in their names:")

    Dim tsk As Task
    For Each tsk In ActiveProject.Tasks
        tsk.Flag1 = (0 < InStr(1, tsk.Name, txt, 1))
    Next tsk

End Sub

推荐阅读