Dec 18, 2015
Macros in Project
Help automate repeated actions
Utilizes Visual Basic for Applications (VBA) – used in other Office Products
Easy to record
Fun to write
Jeff’s Intro to Macros*
Send file – Sensitive Data
Wipe out task and Resource names
* I didn’t write it, just used it
Quick Look at the Code
Sub scrub()'This macro clears the task name, resource name and text fields.'It also resets the project name and title'Copyright Jack Dahlgren Feb. 2002
Dim t As TaskDim ts As TasksDim r As ResourceDim rs As ResourcesDim myok As Integer
myok = MsgBox("This will permanently remove tasknames, resource names and notes from your project. Are you sure you want to continue?", 257, "ERASE DATA?")
If myok = 1 ThenSet ts = ActiveProject.TasksSet rs = ActiveProject.ResourcesFor Each r In ActiveProject.Resources r.Name = r.UniqueID r.Group = "" r.Initials = r.UniqueIDNext r
For Each t In ts If Not t Is Nothing Then t.Name = t.UniqueID t.Notes = "" End IfNext t
End IfEnd Sub
Let’s Take A Look
Sub Sample_Filter_and_Sort()
' Sample for MPUG Presentation
FilterApply
Name:="_flt_Framing_Contractor"
Sort Key1:="Duration", Ascending1:=False
GroupApply Name:="_grp_Floor"
SelectTaskField Row:=1, Column:="Name"
GotoTaskDates
End Sub
Make a similar one for a standard view
A few hints
Modules vs Macros Multiple Macros in a Module
Naming No Spaces Allowed Use Underscore
Comments Lines starting with a ‘ are comments Use these to remind yourself (and
others) what the code is doing
Relationship Between Elements
Macro to Open Form
Form calls to Macro
(Procedure)
Macro GeneratesData
Form DisplaysData
Call to Procedure
Open The FormSub analysis_frm()FilterApply "All Tasks"OutlineShowAllTasksfrm_project_analysis.ShowEnd Sub
Call the ProcedurePrivate Sub UserForm_Initialize()Call project_data
Generate the Data
Sub project_data()Dim tsk As TaskDim tsk_sum As Task
pred = 0succ = 0fnlt = 0snlt = 0sumtasks = 0longtasks = 0lags = 0sumlink = 0
For Each tsk In ActiveProject.Tasks If Not (tsk Is Nothing) Then If tsk.Summary = False Then If tsk.Predecessors = "" Then pred = pred + 1 End If If tsk.Successors = "" Then succ = succ + 1 End If
Form and Elements
Private Sub UserForm_Initialize()Call project_dataTextBox1.Value = predTextBox2.Value = succTextBox3.Value = fnltTextBox4.Value = snltTextBox5.Value =
ActiveProject.Tasks.Count - sumtasksTextBox6.Value =
ActiveProject.ProjectSummaryTask.Duration / 480 & " days"
TextBox9.Value = longtasksTextBox10.Value = sumlinkTextBox11.Value = lagsLabel12 = ActiveProject.Name
End Sub
Project Status Analysis
Issues Actual Start greater than Status Date
I started that tomorrow Scheduled Start Less that Status Date
I will start that yesterday Actual Finish greater than Status Date Schedule Finish Less than Status Date Over Two Weeks behind
A few more hints
Variable Names Use Caps for variable Names Type all lower case -- VB will add caps if correct (error check) See Example
Sub variabletest()Dim NewVariable As String
newwariableNewVariable
End Sub
Compile Errors
Debugging F8 – Step Into Stop – Run to that point in the code
Part of the Code
If t.Summary = False Then
If t.start < ActiveProject.StatusDate And t.PercentComplete = 0 Then
t.Text13 = "Start less than Status"
ElseIf t.finish < ActiveProject.StatusDate And t.PercentComplete <> 100 Then
t.Text13 = "Finish less than Status"
ElseIf t.ActualFinish > ActiveProject.StatusDate And t.ActualFinish <> notapplicable Then
t.Text13 = "Actual > Status"
ElseIf t.ActualStart > ActiveProject.StatusDate And t.ActualStart <> notapplicable Then
t.Text13 = "Actual > Status"
ElseIf (t.RemainingDuration - 9 * 480) > Application.DateDifference(ActiveProject.StatusDate, t.finish)
Then
t.Text13 = "over 2 weeks behind"
Else
t.Text13 = "OK"
End If
End If
Copy Code
Private Sub Project_Open(ByVal pj As Project)
If MsgBox("Do you wish to install the Special MPUG View?", vbYesNo) <> vbYes Then Exit Sub
Alerts False
OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flag1to3"
OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_Inspector"
OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_general"
OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_Frame_Contractor"
OrganizerMoveItem Type:=0, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="special_gantt"
OrganizerMoveItem Type:=9, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="Global.MPT", Name:="Start1"
FileClose pjDoNotSave, True
Alerts True
End Sub
Open Files in File ListSub File_list()
Dim xcel As ObjectSet xcel = CreateObject("Excel.Application")
xcel.Workbooks.Open FileName:= _ "C:\users\jeff\Desktop\MPA Nov 13\MPUG\MPUG_Demo\Mpug_Demo_File_List.xls", _ ReadOnly:=True 'xcel.Visible = TrueFor i = 2 To 10 If xcel.cells(i, 1) = "" Then Exit For FileName = "C:\users\jeff\Desktop\MPA Nov 13\MPUG\MPUG_Demo\" & xcel.cells(i,
1).Value _ & ".mpp" FileOpen FileName, ReadOnly:=TrueNext i
xcel.Quit ' When you finish, use the Quit method to closeSet xcel = Nothing ' the application, then release the reference.
End Sub
Open MPUG Demo File
Last Code (Partial)
Sub Save_Reports()' For Each r In ActiveProject.Resources filtername = "Tasks_for_" + r.Name mapname = filtername + "_" FilterEdit Name:=filtername, TaskFilter:=True, Create:=True, OverwriteExisting:=True, FieldName:="Start", test:="is less than", Value:=filterdate,
ShowInMenu:=False, ShowSummaryTasks:=False FilterEdit Name:=filtername, TaskFilter:=True, FieldName:="", NewFieldName:="Resource Names", test:="contains", Value:=r.Name,
Operation:="And", ShowSummaryTasks:=False FilterEdit Name:=filtername, TaskFilter:=True, FieldName:="", NewFieldName:="% Complete", test:="Does not equal", Value:="100",
Operation:="And", ShowSummaryTasks:=False MapEdit Name:=mapname, Create:=True, OverwriteExisting:=True, _ DataCategory:=pjMapTasks, CategoryEnabled:=True, TableName:="Task_Table", _ FieldName:="Text15", ExternalFieldName:="Project", ExportFilter:=filtername 'MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ 'FieldName:="ID" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Unique ID", ExternalFieldName:="Unique ID" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Name", ExternalFieldName:="Task Name" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Start", ExternalFieldName:="Start_Date" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Finish", ExternalFieldName:="Finish_Date" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Work", ExternalFieldName:="Hours" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Resource Names", ExternalFieldName:="Resources"
savename = mapname + statdate FileSaveAs Name:=Location + savename + ".xls", FormatID:="MSProject.XLS5", Map:=mapname Next r
End Sub
Resources
Websites with Macros http://masamiki.com/project/macros.htm http://www.brighthub.com/office/project-manag
ement/articles/52620.aspx http://visualbasic.about.com/od/learnvba/Learn
_to_program_using_Visual_Basic_for_Applications_VBA.htm
http://zo-d.com/blog/archives/programming.html