MS Project to Excel使用VBA跟随甘特图

时间:2016-12-01 14:23:12

标签: excel vba excel-vba

@ shai-rado

嗨 - 这是

的后续问题

MS Project to Excel Gantt Chart using VBA

Public Sub Excel()
    ' This code will import timesheet data from an Excel spreadsheet as ActualWork in your
    ' project schedule.

    ' Your Excel file must have a named range, "TimeSheetEntries", which has 
    ' 5 columns: EntryNumber, WBS, Employee, Date, and Minutes. 
    ' Don't include any column headers in the named range.

    ' EntryNumber is a unique ID coming from our legacy time sheet application. It will be 
    ' added to the assignment.Notes field, but has does not affect any program logic.

    ' WBS is the key used to reference the appropriate task. This means that either you
    ' or your resources have to enter this code when entering data in their time sheets.

    ' Employee is the resource's name, and it must match up with the resource names in
    ' your assignments.

    ' Date is... the date for the work sheet entry. One entry per day please - this code will
    ' not handle a range of dates.

    ' Minutes is the number of minutes that the resource spent on this particular task, on this 
    ' particular day.

    ' So if Joe Schmo worked for 3 1/2 hours on task WBS-3.2.2 on the 13th of June 2013, and
    ' your timesheet application assigned this entry the identifier "36894", the row in Excel should
    ' look like this:

    '        36894        WBS-3.2.2        Schmo, Joe        06/13/2013        210

    ' If your data contains entries from resources who were not assigned to the entries task, 
    ' their data will not be added. The VBA debug window will contain a list of the unassigned entries.
    ' You need to go back and determine why someone is doing work they were not assigned to.
    ' If the work is valid, you can either add them to the task's resources, or add a new task and
    ' have them resubmit the data with the new WBS code.

    Dim assignment As assignment
    Dim c As Excel.Range
    Dim oExcel As Excel.Application
    Dim oWB As Excel.Workbook
    Dim Row As Excel.Range
    Dim task As task
    Dim tsv As TimeScaleValue
    Dim tsvs As TimeScaleValues
    Dim FoundAssignment As Boolean

    Set oExcel = New Excel.Application
    Set oWB = oExcel.Workbooks.Open(FileName:="path to excel file", ReadOnly:=True)

    For Each Row In Range("TimeSheetEntries").Rows
        FoundAssignment = False
        EntryNumber = Row.Cells(1, 1)
        WBS = Row.Cells(1, 2)
        Employee = Row.Cells(1, 3)
        MyDate = Row.Cells(1, 4)
        Minutes = Row.Cells(1, 5).Value

        FilterEdit Name:=    "Update_Actual_Work", _
                                      TaskFilter:=True, _
                                      Create:=True, _
                                      OverwriteExisting:=True, _
                                      FieldName:="WBS", _
                                      Test:="equals", _
                                      Value:=WBS

        FilterApply Name:="Update_Actual_Work"

        SelectAll

        Set task = ActiveSelection.Tasks(1)

        For Each assignment In task.Assignments
            If (StrComp(assignment.ResourceName, Employee) = 0) Then
                FoundAssignment = True
                Set tsvs = assignment.TimeScaleData(Startdate:=MyDate, EndDate:=MyDate, _
                         Type:=pjAssignmentTimescaledActualWork, TimeScaleUnit:=pjTimescaleDays, _
                         Count:=1)
                tsvs(1).Value = Minutes
                assignment.Notes = assignment.Notes & EntryNumber & vbCrLf
                Exit For
            End If
        Next
        FilterClear
        If Not FoundAssignment Then Debug.Print EntryNumber & vbTab & "not allocated"
    Next

    oWB.Close
    Set oWB = Nothing
    oExcel.Quit

End Sub
  

嗨 - 我是Project Macros的新手,所以我想我会从你的开始   代码如何在Project 2013中运行它我得到一个“Complie错误:   用户定义的类型未在Dim xlApp As Excel.Application上定义   命令。在那里读(stackoverflow.com/questions/19680402 / ...)   似乎已经改变了代码的格式。它是否正确   或者我是否需要在我自己的项目中设置其他位置   认为代码向后兼容?在此先感谢T

我对这段代码也有同样的问题,所以我觉得有一个共同的主题?

SelectMany

1 个答案:

答案 0 :(得分:0)

成功解决了这个基本问题......你需要进入宏观屏幕,找到工具和参考资料 - 根据需要勾选Excel等。

由于

Ť