LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.misc
retseort
 
Posts: n/a
Default Help, need to speed up this macro


Forgive because this will be a lot of code. The overall point to all of
this code is to update the header and footer based upon entires made on
the HeaderPage worksheet. The code pulls the entries made and populates
the header and footer on all worksheets with in the workbook. The issue
is that it has to loop through each worksheet when activated and can
take some time to complete. Is there anything I can do to this to speed
it up?

The code below is found in two parts.

The following code is found in ThisWorkbook:


Code:
--------------------
Private Sub Workbook_BeforePrint(Cancel As Boolean)
'this code repeats the header and footer code for each worksheet
'this code drives the warning for the user if they exceed the number of allowable H/F bytes
'this code is triggered every time a user tries to print or print preview
Const c_intMaxHdrLen As Integer = 232

Dim wkSht As Worksheet

If Range("HdrLen") c_intMaxHdrLen Then
MsgBox "Your Header exceeds 232 characters. Please go back to the header page and reduce the # of Characters"
Cancel = True
Exit Sub
End If

Application.ScreenUpdating = False
For Each wkSht In ThisWorkbook.Worksheets
SetHeader wkSht
Next wkSht
Application.ScreenUpdating = True
End Sub


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'this code repeats the header and footer code for each worksheet
'this code drives the warning for the user if they exceed the number of allowable H/F bytes
'this code is triggered every time a user tries to save
Const c_intMaxHdrLen As Integer = 232

Dim wkSht As Worksheet

If Range("HdrLen") c_intMaxHdrLen Then
MsgBox "Your Header exceeds 232 characters. Please go back to the header page and reduce the # of Characters"
Cancel = True
Exit Sub
End If

Application.ScreenUpdating = False
For Each wkSht In ThisWorkbook.Worksheets
SetHeader wkSht
Next wkSht
Application.ScreenUpdating = True
End Sub
--------------------


The next code is found in Module 1


Code:
--------------------
Sub SetHeader(Sh As Worksheet)
' this code takes data from the header page
'and populates it to the header and footer
Dim lStr As String
Dim rStr As String
Dim dStr As String
Dim eStr As String
Dim tStr As String

With Worksheets("HeaderPage")
Application.ScreenUpdating = False
'defines where the data is coming from on the header page and what the format is
lStr = "&8" & .Range("K2") & vbCr & .Range("K3") & vbCr & .Range("K4") & vbCr & .Range("K5")
rStr = "&8" & .Range("M2") & vbCr & .Range("M3") & vbCr & .Range("M4") & vbCr & .Range("M5") & vbCr & .Range("M6")
dStr = "&8" & .Range("M11")
eStr = "&6" & .Range("W1") & vbCr & .Range("W2") & vbCr & .Range("W3") & vbCr & .Range("W4")
tStr = "Page " & "&P" & " of " & "&N"
End With

With Sh.PageSetup
.LeftHeader = lStr
.CenterHeader = dStr
.RightHeader = rStr
.CenterFooter = eStr
.RightFooter = tStr
End With

With ActiveSheet.PageSetup
'resets the top and bottom margins to accomodate the new header
.TopMargin = Application.InchesToPoints(1.24)
.BottomMargin = Application.InchesToPoints(1)
Sheets("Instructions").Visible = False

End With
End Sub
--------------------


--
retseort
------------------------------------------------------------------------
retseort's Profile: http://www.excelforum.com/member.php...o&userid=24690
View this thread: http://www.excelforum.com/showthread...hreadid=500187

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search, Copy, Paste Macro in Excel [email protected] Excel Worksheet Functions 0 January 3rd 06 06:51 PM
Can T Get Macro To Run! Nipper New Users to Excel 2 November 4th 05 04:48 AM
Closing File Error jcliquidtension Excel Discussion (Misc queries) 4 October 20th 05 12:22 PM
Speed up macro rn Excel Discussion (Misc queries) 3 February 21st 05 01:25 PM
Date macro Hiking Excel Discussion (Misc queries) 9 February 3rd 05 12:40 AM


All times are GMT +1. The time now is 02:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"