Drawing of Stakeholder map

Microsoft Project, Project planning, Templates and Advice

  • Concise, focused guide that cuts through the clutter
  • Step-by-step instructions for creating a project plan in under a day
  • Master essential skills like work breakdowns and task sequencing
  • Real-world troubleshooting for 20 common scheduling challenges
  • Rapidly get up to speed if you're new to Microsoft Project
  • Includes glossary, support resources, and sample plans
The cover of the book 'Essential Microsoft Project: The 20% You Need to Know'

Microsoft Project vba - Microsoft Project Macros

by | reviewed 2024/03/26

Example vba code

Most of these macros are examples that were first published on Jack Dahlgren's website Masamiki. This page seems to have been moved or deleted so I am publishing here for moment. If you know where the original macros are please contact me so I can link to the page and give Jack the proper credit.
  1. Remove non-logic constraints
  2. Restore non-logic constraints
  3. Remove all constraints
  4. Restore constraints
  5. Add notes to multiple tasks
  6. Add text to the start of multiple Task Names
  7. Add text to the finish of multiple Task Names

Macro to Remove non-logic constraints

This macro removes all constraints from tasks with predecessors. A constraint fixes a start to a particular date. For example 'start no earlier than' x date or 'must start on' x date. Often constraints are accidently created by copying and pasting tasks or setting start or finish dates without fully understanding the consequences. Tasks that have predecessors are not likely to also be fixed to a certain date. However, there are exceptions to this, so before you run this macro make sure you understand what constraints are for and the common mistakes in using constraints.
 'Macros to Remove contraints from tasks that are kept in place by logic  
 'This sets the tasks to have a constraint type of 0 which is "As Soon As Possible"  
 'You can reverse it by running Restore_NonLogic_Contraints  
 'Copyright Jack Dahlgren 4/17/00  
 'Tamzin Morphy (https://www.stakeholdermap.com/ms-project/ms-project-vba.html) - Changed = vbNo Then End (causing automation error) to = vbno Then Exit Sub  
 Sub Remove_NonLogic_Constraints()  
 Dim ProjTasks As Tasks  
 Dim ProjTask As Task  
 Dim TaskDeps As TaskDependencies  
 If MsgBox("This Macro will remove All Constraints from tasks that have predecessors. Do you want to continue?", 
 vbYesNo) = vbNo Then Exit Sub  
 Set ProjTasks = ActiveProject.Tasks  
 For Each ProjTask In ProjTasks           'these if statements step through all tasks in the project  
   If Not (ProjTask Is Nothing) Then        'this handles blank line in the file  
     If ProjTask.Summary = False Then      'this handles summary tasks which are only allowed to be fixed duration  
       If ProjTask.ExternalTask = False Then  'this handles external tasks  
       ProjTask.Date1 = ""   'Clears the field we use to restore original constraint date  
       ProjTask.Number15 = 0    'Clear the field we use to restore constraint type  
       If ProjTask.TaskDependencies.Count > 0 Then  'selects tasks with 1 or more dependencies  
         ProjTask.Date1 = ProjTask.ConstraintDate  'Stores restore data into fields  
         ProjTask.Number15 = ProjTask.ConstraintType 'Stores restore data into fields  
         ProjTask.ConstraintType = 0   'Sets task to as soon as possible  
         End If  
       End If  
     End If  
   End If  
 Next ProjTask  
 End Sub  

Macro to Restore non-logic constraints

 'Macro to Restore contraints that were erased by the Remove_NonLogic_Contraints() macro  
 'This sets resets the tasks to have their original constraint types and dates  
 'Copyright Jack Dahlgren 4/17/00  
 'Tamzin Morphy (https://www.stakeholdermap.com/ms-project/ms-project-vba.html)- Changed = vbNo Then End (causing automation error) to = vbno Then Exit Sub  
 Sub Restore_NonLogic_Contraints()  
 Dim ProjTasks As Tasks  
 Dim ProjTask As Task  
 Dim TaskDeps As TaskDependencies  
 If MsgBox("This Macro will restore All Constraints for tasks that have predecessors. Do you want to continue?", vbYesNo) = vbNo Then Exit Sub  
 Set ProjTasks = ActiveProject.Tasks  
 For Each ProjTask In ProjTasks           'these if statements step through all tasks in the project  
   If Not (ProjTask Is Nothing) Then        'this handles blank line in the file  
     If ProjTask.Summary = False Then      'this handles summary tasks which are only allowed to be fixed duration  
       If ProjTask.ExternalTask = False Then  'this handles external tasks  
       If ProjTask.TaskDependencies.Count > 0 Then   'Selects only tasks with dependencies  
         ProjTask.ConstraintType = ProjTask.Number15   'restores data from storage fields  
         ProjTask.ConstraintDate = ProjTask.Date1    'restores data from storage fields  
         End If  
       End If  
     End If  
   End If  
 Next ProjTask  
 End Sub  

Macro to Remove all constraints

 'Macro to remove all task constraints  
 'This sets the tasks to have a constraint type of 0 which is "As Soon As Possible"  
 'You can reverse it by running Restore_NonLogic_Contraints  
 'Tamzin Morphy (https://www.stakeholdermap.com/ms-project/ms-project-vba.html) - Tweak to Remove_NonLogic_Constraints by Jack Dahlgren 4/17/00 - remove all constraints.  
 'Copyright T Morphy 29/07/2014  
 Sub Remove_All_Constraints()  
 Dim ProjTasks As Tasks  
 Dim ProjTask As Task  
 Dim TaskDeps As TaskDependencies  
 If MsgBox("This Macro will remove All Constraints from tasks. Do you want to continue?", vbYesNo) = vbNo Then Exit Sub  
 Set ProjTasks = ActiveProject.Tasks  
 For Each ProjTask In ProjTasks           'these if statements step through all tasks in the project  
   If Not (ProjTask Is Nothing) Then        'this handles blank line in the file  
     If ProjTask.Summary = False Then      'this handles summary tasks which are only allowed to be fixed duration  
       If ProjTask.ExternalTask = False Then  'this handles external tasks  
       ProjTask.Date1 = ""   'Clears the field we use to restore original constraint date  
       ProjTask.Number15 = 0    'Clear the field we use to restore constraint type  
       'If ProjTask.TaskDependencies.Count > 0 Then  'selects tasks with 1 or more dependencies  
         ProjTask.Date1 = ProjTask.ConstraintDate  'Stores restore data into fields  
         ProjTask.Number15 = ProjTask.ConstraintType 'Stores restore data into fields  
         ProjTask.ConstraintType = 0   'Sets task to as soon as possible  
         ' End If  
       End If  
     End If  
   End If  
 Next ProjTask  
 End Sub  

Macro to Restore constraints removed by 'Remove all constraints'

 'Macro to Restore all contraints that were erased by any remove constraints macro  
 'This sets resets the tasks to have their original constraint types and dates  
 'Tamzin Morphy (https://www.stakeholdermap.com/ms-project/ms-project-vba.html) - Tweak to Restore_NonLogic_Constraints by Jack Dahlgren 4/17/00 - restore all constraints  
 'Copyright T Morphy 29/07/2014  
 Sub Restore_All_Contraints()  
 Dim ProjTasks As Tasks  
 Dim ProjTask As Task  
 Dim TaskDeps As TaskDependencies  
 If MsgBox("This Macro will restore All Constraints for tasks that have predecessors. Do you want to continue?", vbYesNo) = vbNo Then Exit Sub  
 Set ProjTasks = ActiveProject.Tasks  
 For Each ProjTask In ProjTasks           'these if statements step through all tasks in the project  
   If Not (ProjTask Is Nothing) Then        'this handles blank line in the file  
     If ProjTask.Summary = False Then      'this handles summary tasks which are only allowed to be fixed duration  
       If ProjTask.ExternalTask = False Then  'this handles external tasks  
       'If ProjTask.TaskDependencies.Count > 0 Then   'Selects only tasks with dependencies  
         ProjTask.ConstraintType = ProjTask.Number15   'restores data from storage fields  
         ProjTask.ConstraintDate = ProjTask.Date1    'restores data from storage fields  
         ' End If  
       End If  
     End If  
   End If  
 Next ProjTask  
 End Sub  

Add notes to multiple, selected tasks

This macro adds notes to the Notes of selected tasks. If a task has existing notes it will insert the text at the start of the Notes field. This is useful if you need to add the same note to multiple tasks.
 Sub AddNotesToSelectedTasks()
'This macro adds notes to the Notes field of selected tasks
'It is useful if you want to add explanatory notes to certain types or groups of tasks
'It will add text at the start Notes field, before any existing notes.
'Copyright Tamzin Morphy July 2014 https://www.stakeholdermap.com/ms-project/ms-project-vba.html
	Dim addnote As String
	Dim UpdateTask As Task
	If ActiveSelection = 0 Then
		MsgBox "Select the tasks that you want to add Notes to"
		Exit Sub
	End If
	addnote = InputBox("Enter text to add to the task notes field - no leading space needed, leave blank to quit")
	If addnote = "" Then Exit Sub
	For Each UpdateTask In ActiveSelection.Tasks
		UpdateTask.Notes = addnote & " " & UpdateTask.Notes
		Next UpdateTask
 End Sub

Add text to the start of multiple task names

This macro prefixes text into the Task Name of all selected tasks. It is useful if you are reusing sections of your schedule and want to rename multiple tasks, for example prefixing a project, sprint or work package name to selected tasks. Select the tasks you want to update before running the macro.

Sub AddTextAtStartOfTask()
'This macro adds text you enter at the start of the name of all selected tasks
'It is useful if you want to identify certain tasks as being part of a group for example "sprint 2".
'Based on AddMyText by Jack Dahlgren, Feb 2002
'Copyright Tamzin Morphy July 2014 https://www.stakeholdermap.com/ms-project/ms-project-vba.html
	Dim addtext As String
	Dim UpdateTask As Task
	If ActiveSelection = 0 Then
		MsgBox "Select the tasks that you want to add text to"
		Exit Sub
	End If
	addtext = InputBox("Enter text to add to the start of the task name - no leading space needed, leave blank to quit")
	If addtext = "" Then Exit Sub
	For Each UpdateTask In ActiveSelection.Tasks
		UpdateTask.Name = addtext & " " & UpdateTask.Name
		Next UpdateTask
	End Sub

Add text to the end of multiple task names

This macro appends text you enter onto the name of all selected tasks. It is useful if you are reusing sections of your schedule and want to rename multiple tasks, for example append something like "Phase 2" to selected tasks. Select the tasks you want to update before running the macro.
Sub AppendMyText()
'This macro appends text you enter onto the name of all selected tasks.
'It is useful if you are reusing sections of your schedule and want
'to rename tasks (ie: append something like "Phase 2" to all selected tasks.
'Copyright Jack Dahlgren, Feb 2002
'Tamzin Morphy (https://www.stakeholdermap.com/ms-project/ms-project-vba.html) - added check for selected task and message to select the tasks which will have text appended
	
	Dim newstuff As String
	Dim t As Task
	Dim s As Tasks
	If ActiveSelection = 0 Then
		MsgBox "Select the tasks that you want to append text to"
		Exit Sub
	End If
	newstuff = InputBox("Enter text to add the end of the task name - no leading space needed, leave blank to quit")
	If newstuff = "" Then Exit Sub
	For Each t In ActiveSelection.Tasks
		t.Name = t.Name & " " & newstuff
		Next t
	End Sub

Read more guides on using Microsoft Project

Recommended reading on Microsoft Project

Marmel, E. (2010). Project 2010 Bible, John Wiley & Sons

Daley, S. (2013). Project 2013 In Depth, Que.