如何将列从MS Excel添加到MS Project - VBA

时间:2017-06-19 15:40:15

标签: excel vba excel-vba microsoft-project-vba

我正在编写一个宏来将MS Excel工作表中的一列数据添加到现有的MS Project文档中。当我运行我的宏时,正确的项目被打开,但是我收到一条错误消息,说我打开的项目已被删除,即使它尚未被删除。

这是我当前的宏:

Sub newProjectColumn()
Dim appProj As MSProject.Application
Dim aProg As MSProject.Project
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Set wb = ActiveWorkbook

'~~> Open MS Project
Set ws = wb.Sheets("Sheet1")

Set appProj = CreateObject("Msproject.Application")

appProj.FileOpen "Project_1.mpp"

Set aProg = appProj.ActiveProject

appProj.Visible = True

'~~> Next, create new Column in project
TableEditEx Name:="Project_1.mpp", TaskTable:=True, _
    NewFieldName:="Actual Duration", Title:="Actual Duration", Width:=12, _
    ShowInMenu:=True, _
    ColumnPosition:=29

'~~> Next, copy T:T from Excel.Then, paste to 29th column (i,29) in Project.
Set Rng = ws.Range("T:T")
ActiveSheet.Paste Destination:=Rng
SelectTaskColumn Column:="Actual Duration"

End Sub

对于我做错了或者有更好的方法的任何帮助都将非常感激!

1 个答案:

答案 0 :(得分:2)

以下是更新“实际持续时间”列所需的代码。根据需要更改表名。根据需要更新范围参考;不包括标题单元格。

    if ($form_state['num_names_3'] > 1) {
      $form['dropdown_fieldset']['remove_name_3'] = array(
        '#type' => 'submit',
        '#value' => t('Remove one 3'),
        '#submit' => array('order_wood_3_add_more_remove_one'),
        '#ajax' => array(
          'callback' => 'order_wood_3_add_more_callback',
          'wrapper' => 'dropdown_fieldset',
        ),
      );
    }

请注意,使用 ' add Actual Duration as the first column & apply the table changes appProj.TableEditEx Name:="Entry", TaskTable:=True, _ NewFieldName:="Actual Duration", Title:="Actual Duration", Width:=12, ColumnPosition:=0 appProj.TableApply "Entry" ' copy updated values from Excel (no headers) and paste in new column in Project Range("T2:T100").Copy appProj.SelectColumn 2 appProj.EditPaste ' delete the column appProj.ColumnDelete 方法添加列时,索引从0开始,而使用TableEditEx方法选择列时,索引从2开始。

TableEditEx method

SelectColumn method