+ Reply to Thread
Results 1 to 13 of 13

Code Runs Very Slow

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Code Runs Very Slow

    I have an Excel Workbook that someone added some coding to a long time ago. This code runs extremely slow and hangs every machine up for 5 seconds or so when it runs. When auto-save is enabled in Excel, the file becomes almost unusable because of this. The only thing I have modified is adding the ScreenUpdating stuff to the beginning and end of each sub, but that had little to no effect. Here is the code:

    Sub Worksheet_Footer()
    
    Application.ScreenUpdating = False
    
    For i = 1 To Worksheets.Count
    doLayout (i)
    Next i
    
    Dim ws As Worksheet
        
        For Each ws In ThisWorkbook.Worksheets
            If Not ws.Name <> "Cover Sheet" Then
                With ws.PageSetup
                    .RightHeader = ""
                    .LeftHeader = ""
                    .LeftFooter = ""
                    .RightFooter = ""
                End With
            End If
        Next ws
    
    Application.ScreenUpdating = True
    
    End Sub
          
    
    Function doLayout(Index As Integer)
    
    Application.ScreenUpdating = False
    
    With Worksheets(Index).PageSetup
    .LeftHeader = "&""Century Gothic""&12" & Chr(13) & Sheet1.Range("$B$4") & Space(1) & Sheet1.Range("$B$5") & Space(1) & Sheet1.Range("$B$6") & Space(1) & ":" & Space(1) & Sheet1.Range("$B$7")
    .LeftFooter = "&""Century Gothic""&8" & "&B Rev: &B" & Sheet1.Range("$B$8") & Space(1) & "-" & Space(1) & Sheet1.Range("$B$9") & Space(5) & "&B Designer: &B" & Space(1) & Sheet1.Range("$B$2").Value & Space(5) & "&B Phone: &B" & Space(1) & "(972) 392-3202" & Space(5) & "&B Security:&B" & Space(1) & "TX B14470 | TX ACR-1776026 | AR CMPY.0002197" & b & Space(5) & "&B Printed: &B" & Space(1) & DateValue(Now)
    .RightFooter = "&""Century Gothic""&8" & "Page &P of &N"
    End With
    
    Application.ScreenUpdating = True
    
    End Function
    Any ideas for how we can get this to run a little smoother/faster?? Thanks in advance for the help!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,833

    Re: Code Runs Very Slow

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,557

    Re: Code Runs Very Slow

    If you initially set your printer to PDF and just before printing, set it back to whichever printer you use.
    Or leave it at PDF and save it as a PDF type file.
    Does that make a difference?

  4. #4
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    Quote Originally Posted by jolivanes View Post
    If you initially set your printer to PDF and just before printing, set it back to whichever printer you use.
    Or leave it at PDF and save it as a PDF type file.
    Does that make a difference?
    Thank you for the suggestion. Unfortunately, I'm not sure how to do what you're saying...

  5. #5
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    I have attached the workbook. Had to zip it to get it under the max file size.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,253

    Re: Code Runs Very Slow

    Setting multiple print page properties is a lengthy process, because immediately after changing one property, a message is sent to the printer driver and the program waits for a response. Only then is the next property set. Therefore, in earlier versions of Excel, macro 4.0 was used. I do not remember from which version (maybe 2007) the possibility of disconnecting the connection with the printer was introduced for the time of changing the print page settings.
    Try this modified code:
    Sub Worksheet_Footer()
    
        Dim ws          As Worksheet
        Dim i           As Long
    
        Application.ScreenUpdating = False
        Application.PrintCommunication = False
    
        For i = 1 To Worksheets.Count
            doLayout i
        Next i
    
        For Each ws In ThisWorkbook.Worksheets
            If Not ws.Name <> "Cover Sheet" Then
                With ws.PageSetup
                    .RightHeader = ""
                    .LeftHeader = ""
                    .LeftFooter = ""
                    .RightFooter = ""
                End With
            End If
        Next ws
    
        Application.PrintCommunication = True
        Application.ScreenUpdating = True
    
    End Sub
    
    
    Sub doLayout(Index As Integer)
    
    
        With Worksheets(Index).PageSetup
            .LeftHeader = "&""Century Gothic""&12" & Chr(13) & Sheet1.Range("$B$4") & Space(1) & Sheet1.Range("$B$5") & Space(1) & _
                          Sheet1.Range("$B$6") & Space(1) & ":" & Space(1) & Sheet1.Range("$B$7")
    
            .LeftFooter = "&""Century Gothic""&8" & "&B Rev: &B" & Sheet1.Range("$B$8") & Space(1) & "-" & Space(1) & _
                          Sheet1.Range("$B$9") & Space(5) & "&B Designer: &B" & Space(1) & Sheet1.Range("$B$2").Value & Space(5) & _
                          "&B Phone: &B" & Space(1) & "(972) 392-3202" & Space(5) & "&B Security:&B" & Space(1) & _
                          "TX B14470 | TX ACR-1776026 | AR CMPY.0002197" & b & Space(5) & "&B Printed: &B" & Space(1) & Format(Date)
    
            .RightFooter = "&""Century Gothic""&8" & "Page &P of &N"
        End With
    
    
    End Sub
    Artik

  7. #7
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    Quote Originally Posted by Artik View Post
    Setting multiple print page properties is a lengthy process, because immediately after changing one property, a message is sent to the printer driver and the program waits for a response. Only then is the next property set. Therefore, in earlier versions of Excel, macro 4.0 was used. I do not remember from which version (maybe 2007) the possibility of disconnecting the connection with the printer was introduced for the time of changing the print page settings.
    Try this modified code:
    Sub Worksheet_Footer()
    
        Dim ws          As Worksheet
        Dim i           As Long
    
        Application.ScreenUpdating = False
        Application.PrintCommunication = False
    
        For i = 1 To Worksheets.Count
            doLayout i
        Next i
    
        For Each ws In ThisWorkbook.Worksheets
            If Not ws.Name <> "Cover Sheet" Then
                With ws.PageSetup
                    .RightHeader = ""
                    .LeftHeader = ""
                    .LeftFooter = ""
                    .RightFooter = ""
                End With
            End If
        Next ws
    
        Application.PrintCommunication = True
        Application.ScreenUpdating = True
    
    End Sub
    
    
    Sub doLayout(Index As Integer)
    
    
        With Worksheets(Index).PageSetup
            .LeftHeader = "&""Century Gothic""&12" & Chr(13) & Sheet1.Range("$B$4") & Space(1) & Sheet1.Range("$B$5") & Space(1) & _
                          Sheet1.Range("$B$6") & Space(1) & ":" & Space(1) & Sheet1.Range("$B$7")
    
            .LeftFooter = "&""Century Gothic""&8" & "&B Rev: &B" & Sheet1.Range("$B$8") & Space(1) & "-" & Space(1) & _
                          Sheet1.Range("$B$9") & Space(5) & "&B Designer: &B" & Space(1) & Sheet1.Range("$B$2").Value & Space(5) & _
                          "&B Phone: &B" & Space(1) & "(972) 392-3202" & Space(5) & "&B Security:&B" & Space(1) & _
                          "TX B14470 | TX ACR-1776026 | AR CMPY.0002197" & b & Space(5) & "&B Printed: &B" & Space(1) & Format(Date)
    
            .RightFooter = "&""Century Gothic""&8" & "Page &P of &N"
        End With
    
    
    End Sub
    Artik

    Thank you! Unfortunately, when I replace my code with the code you provided, I get the following error:

    Screenshot.png

    I tried just using the PrintCommunication part (set to False, then back to True) and it seemed to slightly improve, but still hangs for a few seconds.

    Thanks again!

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,253

    Re: Code Runs Very Slow

    Quote Originally Posted by xTyD23x View Post
    Thank you! Unfortunately, when I replace my code with the code you provided, I get the following error:
    It's just a type mismatch. Change the procedure declaration to
    Sub doLayout(Index As Long)
    But your further statement says that this is unlikely to help in a significant acceleration of the action.

    Artik

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,557

    Re: Code Runs Very Slow

    This is for Windows 10. I don't have 365.
    https://support.microsoft.com/en-us/...4-c41022b5036f
    Select the "Microsoft Print To PDF" and then run your macro. It might or might not make a difference but when using PageSetup, it continuously communicates with the printer which slows thing down. Try it.

  10. #10
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    Quote Originally Posted by jolivanes View Post
    This is for Windows 10. I don't have 365.
    https://support.microsoft.com/en-us/...4-c41022b5036f
    Select the "Microsoft Print To PDF" and then run your macro. It might or might not make a difference but when using PageSetup, it continuously communicates with the printer which slows thing down. Try it.
    Thanks for the suggestion, but I'm looking for a solution that will fix this issue on multiple user's machines so I'd like to get this code optimized. Your solution could be a nice work-around for the time being though, so thank you!

  11. #11
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    I went back and forth between using the PrintCommunication False/True statements, and it definitely makes a big difference to have those in there. It isn't perfect right now, but it's already improved a bunch, so thanks again! If anyone else has any suggestion as to how it could be even better I'm all ears. Thank you!

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,833

    Re: Code Runs Very Slow

    Historically, Page Setup has always been horrendously slow. So much so that, typically, it has often been recommended to use Excel4 macros.

    See: https://msgroups.net/excel.programmi...xcel4-ma/21423

    Google: excel4 macro for printer settings for more information/examples

  13. #13
    Registered User
    Join Date
    02-18-2020
    Location
    Dallas TX
    MS-Off Ver
    365
    Posts
    33

    Re: Code Runs Very Slow

    Quote Originally Posted by TMS View Post
    Historically, Page Setup has always been horrendously slow. So much so that, typically, it has often been recommended to use Excel4 macros.

    See: https://msgroups.net/excel.programmi...xcel4-ma/21423

    Google: excel4 macro for printer settings for more information/examples
    Thank you, I'll check this out. Really appreciate the help guys!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My code does everything it should, but runs VERY slow!
    By zookeepertx in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-28-2020, 01:56 AM
  2. VBA code runs slow
    By jamfz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2016, 12:25 PM
  3. [SOLVED] VBA code runs slow when other workbooks are open
    By JimDandy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-27-2016, 12:47 AM
  4. VBA Code runs too slow
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2012, 07:19 AM
  5. Code runs slow until I bring some other app to foreground
    By patatvs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-02-2011, 11:48 AM
  6. Fibonacci Code Runs too slow
    By MarvinP in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-20-2010, 02:14 PM
  7. VBA code runs slow until I push ESC
    By MCCCLXXXV in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-04-2007, 11:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1