如何使用宏将数据从mpp文件复制到excel

时间:2012-02-19 08:51:04

标签: excel-vba ms-project vba excel

我是macros的新手。我想编写一个宏,使用excel将MPP中的列中的特定数据复制到另一个。

我找到了一个将数据从一个excel复制到另一个excel的代码。请帮助

Option Explicit

Sub CopytoPS()
    Dim sfil As String
    Dim owbk As Workbook
    Dim sPath As String

    sPath = "C:\Users\HYMC\Excel\Test\" 'Change the file path for your purposes
    sfil = Dir(sPath & "Management Report PS.xls")

    Range("A2:I22").Copy

    Set owbk = Workbooks.Open(sPath & sfil)
    owbk.Sheets("Sales Data").Range("B65536").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    owbk.Close True 'Save opened workbook and close
    sfil = Dir
End Sub

我想将MPP中的某些coloums复制到Excel中的一组cloum中。我还希望用户只提供目标文件路径,源文件,要复制的源单元格和目标单元格

1 个答案:

答案 0 :(得分:0)

要在Excel中使用MPP文件,请打开VBA编辑器,然后单击“工具”菜单上的“引用”。在“可用引用”列表中,单击以选中“Microsoft Project xx.xx对象库”复选框。如果未列出Microsoft Project 9.0对象库,请单击“浏览”以找到MsprjXX.olb文件,该文件位于安装Microsoft Project的文件夹中。默认位置是C:\ Program Files \ Microsoft Office \ Office。单击“确定”关闭“引用”对话框。然后使用此代码。

由于您尚未提及要复制的内容以及确切位置,因此我将为您提供一个非常基本的代码,然后您可以使用该代码。

'~~> Code to open MPP file in Excel
Sub Sample()
    Dim appProj As MSProject.Application
    Dim aProg As MSProject.Project
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ActiveWorkbook

    '~~> This is the Sheet Where you want the data to be copied
    Set ws = wb.Sheets("Sheet1")

    Set appProj = CreateObject("Msproject.Application")

    '~~> This is a MS Project File. Change path as applicable.
    appProj.FileOpen "C:\MS Project.mpp"

    Set aProg = appProj.ActiveProject

    appProj.Visible = True

    '~~> Now you have the MPP file opened, rest of the code goes here
End Sub
  
    
      

prerna:你能否为我提供学习在excel中使用的宏的教程。这将非常有帮助

    
  

您可以访问此链接,这是一个良好的开端。但最终,这一切都取决于你练习多少:)

主题:记录并使用Excel宏

链接http://office.microsoft.com/en-us/excel-help/record-and-use-excel-macros-HA001054837.aspx

更多关于宏

http://www.excel-vba.com/

http://www.excel-vba-easy.com/

http://www.mrexcel.com/articles.shtml