Slow exports from MS Project to Excel
Eric,
Nice work but I would recommend decreasing the WITH group dot parsing by
using:
WITH xlSheet.PageSetup
.RightMargin = 25
.LeftMargin = 25
....
END WITH
instead of what you currently used. The two lines that don't have
..PageSetup could be put in their own With grouping or just left as
individual statements e.g. xlSheet.Name = "All Tasks for Team". I read in
an Excel book for professionals (sorry MVPs, I don't remember the three
authors as the book is at home) that this can help save processing time.
Others,
To specifically illuminate what Eric is referring to about screen updating:
xlApp.ScreenUpdating = False
This turns off the screen so you won't see it update as the code goes along.
This can really help speed up code execution.
xlApp.ScreenUpdating = True
This will turn on the screen updating. If code is running and modifying
stuff on your worksheet then you will see it get updated. Typically the
screen will blink when code is running and screen updating is on.
I've heard the argument from some coders (but not end users!) that they
don't like to turn off screen updating because then the user knows Excel is
"working". I disagree and point to a more elegant "Excel is working"
method:
Application.StatusBar = "Hey, Excel is working so please wait..."
or whatever text you wish to use:
sMessage = "Working on file " & sFileName
Application.StatusBar = sMessage
and when you're done you can return the status bar back to normal:
Application.StatusBar = False
Naturally, if you want to get more whiz-bang, you can use a form to display
messages. I don't often cuz that's more work & things to go wrong, no fun
to debug for others, etc.
--
Toby Erkson
Excel 2003, WinXP
"EricG" wrote in message
...
I modified your code a little. This version should speed things up
considerably. It does two things. First, it uses
"Application.ScreenUpdating" to turn off/on Excel screen updating. Any
time
you do things like formatting cells, it slows Excel down a bunch. Second,
I
first pull all the task information into memory to manipulate it in one
big
array, and the place the entire contents of the array onto the Excel
worksheet. That's quicker than doing things cell-by-cell. Finally, I do
some of the formatting on entire columns or rows, again better than the
cell-by-cell approach. Try it and feed back your results. It sped up my
small test project by a factor of three. If it works, you can use the
code
as an example of what you can do to all of your macros to speed them up.
HTH,
Eric
Option Explicit
Option Base 1
Sub AllTaskstoExcel()
'===========================================
' This macro exports all project tasks to a single Excel worksheet tab.
'===========================================
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRow As Excel.Range
Dim xlCol As Excel.Range
Dim Proj As Project
Dim T As Task
Dim ts As Tasks
Dim time1 As Double
Dim Asgn As Assignment
Dim Columns As Integer
Dim calcFinishDAte As Variant
Dim myStartDate As Date
Dim ProjName As String
If xlApp Is Nothing Then
'Start new instance
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Can't Find Excel, please try again.", vbCritical
End 'Stop, can't proceed without Excel
End If
Else
Set xlRow = Nothing
Set xlCol = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Can't Find Excel, please try again.", vbCritical
End 'Stop, can't proceed without Excel
End If
End If
xlApp.ScreenUpdating = False
time1 = Timer()
Set xlBook = xlApp.Workbooks.Add
AppActivate "Microsoft Project"
xlApp.Visible = True
AppActivate "Microsoft Excel"
'Get the Project Name to be used in the Page Header
'You need to change the project name in the "GetProjectName" macro/module
'#######################################
' NOTE: I changed the following line!
'#######################################
ProjName = ThisProject.Name
Set xlSheet = xlBook.Worksheets.Add
With xlSheet
.Name = "All Tasks for Team" ' Description for the Excel worksheet tab
.PageSetup.CenterHeader = "&B &14" + ProjName + "&B" ' Makes the header
you entered bold and 14 pt font size
.PageSetup.RightMargin = 25
.PageSetup.LeftMargin = 25
|