Thanks Jack,
When I click on my current project in the top left window and view
references, it is V11. When I click on global and view references I only see
V5. This is in the Project VBA session. In Excel it is V11. Given that the
error I get references '_Global', this seems to me to be theprime culprit.
I have changed all of my Range statements into Excel.Range, but it has not
fixed the problem.
I have not added a specific line to activate the sheet. The code follows
directly from your heirarch macro (before it throws back to Project), and I
always close down Excel before I run it, so I am always in the same state
when I start.
If the problem was due to the sheet not being active, the problem would
happen every time, wouldn't it? I have this issue every other time it starts.
Here is the full code, including the Heirarchy macro which I have made a
couple of tweaks to.
Sub TaskHierarchy()
Dim xlApp As ......
......Dim Acount As Integer
Set xlApp = New Excel.Application
xlApp.Visible = True
AppActivate "Microsoft Excel"
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = ActiveProject.Name
'count columns needed
ColumnCount = 0
For Each t In ActiveProject.Tasks
If Not t Is Nothing Then
If t.OutlineLevel > ColumnCount Then
ColumnCount = t.OutlineLevel
End If
End If
Next t
'Set Range to write to first cell
Set xlRow = xlApp.ActiveCell
'xlRow = "Filename: " & ActiveProject.Name <---I don't want this info on the
sheet.
'dwn 1
'xlRow = "OutlineLevel"
'dwn 1
'label Columns
For Columns = 1 To (ColumnCount + 1)
Set xlCol = xlRow.Offset(0, Columns - 1)
xlCol = Columns - 1
Next Columns
rgt 2
xlCol = "#"
rgt 1
xlCol = "Duration"
rgt 1
xlCol = "Start"
rgt 1
xlCol = "Finish"
rgt 1
xlCol = "Predecessors"
rgt 1
xlCol = "Business Function"
rgt 1
xlCol = "Resource"
rgt -7 - ColumnCount 'Dependant on column count
xlCol = "Task Name" <--- Added to complete header row.
dwn 1
Tcount = 0
Acount = 0
For Each t In ActiveProject.Tasks
If Not t Is Nothing Then
'dwn 1 <--- Did not match how I want it formatted
Set xlCol = xlRow.Offset(0, t.OutlineLevel)
xlCol = t.Name
If t.Summary Then
xlCol.Font.Bold = True
dwn 1
End If
For Each Asgn In t.Assignments
'dwn 1 <--- Did not match how I want it formatted.
Set xlCol = xlRow.Offset(0, Columns)
If Acount = 0 Then
xlCol = Asgn.TaskID
rgt 1
xlCol = (Asgn.Work / 480) & " Days"
rgt 1
xlCol = Asgn.Start
rgt 1
xlCol = Asgn.Finish
rgt 1
xlCol = t.Predecessors
rgt 1
xlCol = t.Text1
rgt 1
Else <--- For loop added in to format where more than one
resource is against a task
rgt 6
End If
xlCol = Asgn.ResourceName
dwn 1
Acount = Acount + 1
Next Asgn
Tcount = Tcount + 1
Acount = 0
End If
Next t
'Delete first column <----Start of my formatting routine
Excel.Range("A:A").Select <---- Method 'Range' of object '_Global' failed
Selection.Delete Shift:=xlToLeft
'Delete first blank
Selection.Offset(0, ColumnCount).Range("A:A").Select
Selection.Delete Shift:=xlToLeft
'Format task number
Selection.Cut
Excel.Range("A:A").Select
Selection.Insert Shift:=xlToRight
Excel.Range("A:A").EntireColumn.AutoFit
Excel.Range("A:A").HorizontalAlignment = xlCenter
'Clear column numbers
Excel.Range(Cells(1, 3), Cells(1, ColumnCount + 1)).Clear
'Resizes task columns
Excel.Range(Cells(1, 2), Cells(1, ColumnCount)).ColumnWidth = 2.14
Selection.Offset(0, ColumnCount).EntireColumn.Select
Selection.EntireColumn.AutoFit
'Sort out predecessor column
Selection.Offset(0, 4).EntireColumn.Select
Selection.ColumnWidth = 13.57
Selection.HorizontalAlignment = xlLeft
'Format start and finish dates
Excel.Range(Cells(1, ColumnCount + 3), Cells(1, ColumnCount +
4)).EntireColumn.Select
Selection.NumberFormat = "d-mmm-yy"
Selection.HorizontalAlignment = xlCenter
Selection.EntireColumn.AutoFit
'Adjust column width for last two columns
Selection.Offset(0, 3).EntireColumn.Select
Selection.EntireColumn.AutoFit
'Merges task name cells
Excel.Range(Cells(1, 2), Cells(1, ColumnCount + 1)).Select
Selection.Merge
'Makes header row bold
Rows("1:1").Select
Selection.Font.Bold = True
'AppActivate "Microsoft Project"
MsgBox ("Macro Complete with " & Tcount & " Tasks Written")
End Sub
Sorry for the length, and thanks for the help!
James