Export from Excel to MS Project and Indenting

VanderBoon

New Member
Joined
Aug 19, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have an Excel file while will be populated with various tasks associated with specific projects structured as below:

1717769983103.png


I am trying to write a macro to export data within to sheet to MS Project, with the programs forming a container for sub tasks as below:

1717770335911.png


Here's an extract I have of the code so far, I'm struggling with a way of using the program names as level one tasks whilst also looping through to pull the underlying tasks in.

VBA Code:
Sub Test()
Dim PrjApp As Object
Dim NewPrj As Object

Set PrjApp = CreateObject("MSProject.Application")
Set NewPrj = PrjApp.Projects.Add

Dim wst As Worksheet

Set wst = ThisWorkbook.Worksheets("Planned Tests")

Dim i As Byte
Dim j As Long
Dim s As Byte
Dim PrjNamePrev As String
Dim PrjNameCur As String

PrjApp.ProjectSummaryInfo Calendar:="24 Hours"

i = 2

NewPrj.Tasks.Add
NewPrj.Tasks(i - 1).OutlineLevel = 1
NewPrj.Tasks(i - 1).Name = wst.Cells(i, 2)
NewPrj.Tasks(i - 1).ResourceNames = wst.Cells(i, 3)

NewPrj.Tasks.Add
NewPrj.Tasks(i).OutlineLevel = 2
NewPrj.Tasks(i).Name = wst.Cells(i, 5)
NewPrj.Tasks(i).Start = CDate(wst.Cells(i, 7))
NewPrj.Tasks(i).Finish = CDate(wst.Cells(i, 8))
NewPrj.Tasks(i).ResourceNames = wst.Cells(i, 3)

j = Sheets("Planned Tests").Range("F" & Rows.Count).End(xlUp).Row

For i = 3 To j

    PrjNamePrev = wst.Cells(i - 1, 2)
    PrjNameCur = wst.Cells(i, 2)

If PrjNameCur = PrjNamePrev Then



    NewPrj.Tasks.Add
    NewPrj.Tasks(i).OutlineLevel = 2
    NewPrj.Tasks(i).Name = wst.Cells(i, 5)
    NewPrj.Tasks(i).Start = CDate(wst.Cells(i, 7))
    NewPrj.Tasks(i).Finish = CDate(wst.Cells(i, 8))
    NewPrj.Tasks(i).ResourceNames = wst.Cells(i, 3)

Else

    NewPrj.Tasks.Add
    NewPrj.Tasks(i).OutlineLevel = 1
    NewPrj.Tasks(i).Name = wst.Cells(i, 2)
    NewPrj.Tasks(i).ResourceNames = wst.Cells(i, 3)
        
    NewPrj.Tasks.Add
    NewPrj.Tasks(i + 1).OutlineLevel = 2
    NewPrj.Tasks(i + 1).Name = wst.Cells(i, 5)
    NewPrj.Tasks(i + 1).Start = CDate(wst.Cells(i, 7))
    NewPrj.Tasks(i + 1).Finish = CDate(wst.Cells(i, 8))
    NewPrj.Tasks(i + 1).ResourceNames = wst.Cells(i, 3)
    
   


Stop

End If

Next i
End Sub

I know the issue, as it overwrites line 7 in MS Project with Task 6, etc, but cannot figure out how to overcome this, or a better way of writing the macro.

1717770887275.png


I'm a relative Noob when it comes to VBA, as you can probably tell! But any help would be greatly appreciated.

Thank you!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You can record macros in MS Project and perhaps looking to import the spreadsheet via a recorded macro in Project could assist with the code that could be adjusted to export from Excel VBA.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,991
Members
452,541
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top