Search code examples
vbams-project

How I can search multiple task names using ms project-vba?


How do I search multiple task names using the MS-Project VBA, given that I have multiple task names copied to clipboard and are divided in to array items by Split.

This code I have searches for one task at a time, giving out the task ID and task name. What I want to do is look for multiple task IDs in a single time.

Simply what I want to do is search the whole array x() in ActiveProject.Tasks to find matches.

Sub NameExample()
Dim t As Task
Dim x() As String
Dim y As String
Dim p As Variant
Dim q As String

Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
p = MyData.GetText
x = Split(p, vbCrLf)



    For Each t In ActiveProject.Tasks
        If InStr(1, t.Name, x(0), 1) Then
           y = y & vbCrLf & t.ID & ": " & t.Name
        End If
    Next t

    If Len(y) = 0 Then
        MsgBox "No tasks with the text "
    Else
        MsgBox y


   End If

End Sub

Solution

  • Solved it!

    Sub NameExample()
    Dim t As Task
    Dim x() As String 
    Dim p As String
    Dim q As Variant
    
    Dim MyData As DataObject
    Dim strClip As String
    
    Set MyData = New DataObject
    MyData.GetFromClipboard
    p = MyData.GetText
    x = Split(p, vbCrLf)
    
    For Each q In x
        For Each t In ActiveProject.Tasks
            If InStr(1, t.Name, q, 1) Then
                y = y & vbCrLf & t.ID & ": " & t.Name
            End If
        Next t
    Next q
    
        If Len(y) = 0 Then
            MsgBox "No tasks with the text "
        Else
            MsgBox y
        End If
    
    End Sub