如何在 VBA MS Project 中获取单元格的值

How to get the value of a cell in VBA MS Project

我需要一个代码来根据项目中某些特定单元格的不同值给出消息,我已经在 Excel 中使用过 VBA,但从未在项目中使用过,所以我不不知道怎么做

我在 VBA Excel 中做了一个代码,它做的事情与我需要在 Project 中做的相同,但我需要代码,但需要一个项目的信息,但是函数我在Excel中使用,没有在项目中定义,所以我不知道如何添加代码

Option Explicit
Private Sub Avance()
Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim i As Integer
Dim ListaTT As String


For i = 3 To 40

        FechaPI = Round(Cells(i, 2).Value, 2)
        FechaPF = Cells(i, 3).Value
        FechaRI = Cells(i, 4).Value
        FechaRF = Cells(i, 5).Value
        PtjR = Round(Cells(i, 6).Value, 2)
        PtjP = Round(Cells(i, 7).Value, 2)

        If PtjR < PtjP Then

                ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & Cells(i, 1).Value & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"

                If FechaPF - DateValue(Now) < 0 Then
                    ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
                ElseIf FechaPF - DateValue(Now) <= 7 Then
                    ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
                End If


        End If

Next i

MsgBox ListaT, vbCritical, "Advertencia"



End Sub

我不确定获取单元格值的方式是否是唯一会改变代码的方式,但知道如何做到这一点将大有帮助

要获取任务的值,请使用 Task object. In this case you'll want to loop through all of the tasks using the Tasks 对象(所有任务的集合)。目前还不清楚您需要哪些任务字段,但这应该可以帮助您入门:

Private Sub Avance()

Dim PtjR As Double
Dim PtjP As Double
Dim FechaRI As Date
Dim FechaRF As Date
Dim FechaPI As Date
Dim FechaPF As Date
Dim ListaT As String
Dim t As Task

For Each t In ActiveProject.Tasks

    FechaPI = t.Start
    FechaPF = t.Finish
    FechaRI = IIf(t.BaselineStart = "NA", 0, t.BaselineStart)
    FechaRF = IIf(t.BaselineFinish = "NA", 0, t.BaselineFinish)
    PtjR = t.PercentComplete
    PtjP = t.PhysicalPercentComplete

    If PtjR < PtjP Then

        ListaT = ListaT & vbNewLine & Chr(13) & "La tarea" & " " & t.Name & " " & "se encuentra en DELATE, lleva" & " " & PtjR * 100 & "% y debería llevar" & " " & PtjP * 100 & "%"

        If FechaPF - DateValue(Now) < 0 Then
            ListaT = ListaT & ". Esta tarea debió terminar hace " & -(FechaPF - DateValue(Now)) & " días."
        ElseIf FechaPF - DateValue(Now) <= 7 Then
            ListaT = ListaT & ". Esta tarea termina en " & FechaPF - DateValue(Now) & " días."
        End If

    End If

Next t

MsgBox ListaT, vbCritical, "Advertencia"

End Sub

请注意,Percent Complete and Physical Percent Complete 属性 return 是一个从 0 到 100 的值,所以以后不要乘以 100。