alexsheehan
Member
Hi everyone
Apologies in advance for the lengthy post, my code is quite long.
Could someone please take a look at the code below for me and firstly, see if they can fix a couple of things that aren't quite working the way i want them to, and secondly, see if the code can be streamlined in any way as although I have it working in a fashion, I'm not sure if it is the most efficient way of coding this (I'm relatively new to VBA - sorry).
Firsttly, what I am trying to achieve is copy a range from one worksheet to another (this is successful) then I am trying to set the print range automatically and printout the results restricted to the used data range and also setting the page-breaks to specified number of rows (this I can do... kind of) and this is the part I need help with. Basically i want the code below to set page-breaks every 58 rows and this is fine, however, for some reason, the first page is only 56 rows and it's throwing the rest of the pages out.
Secondly, my progress bar was working fine at one stage and now it seems to have malfunctioned, it progresses to teh first iteration bu then stops. So for example, if I have 6 pages printing, it will start off at 0%, then progress to 17% and then stop - the code still completes but there is no visual indication of this to the user.
Sub Main()
Dim Counter As Integer
Dim PctDone As Single
Dim StartRow As Integer
Dim EndRow As Integer
Dim Msg As String
Dim i As Integer
Dim PDFFileName As String
Dim MySheet As Worksheet
If StartRow > EndRow Then
Msg = "ERROR" & vbCrLf & "The starting row must be less than the ending row!"
MsgBox Msg, vbCritical, APPNAME
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Form".Activate
StartRow = Range("StartRow"
EndRow = Range("EndRow"
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
'===============================Clear Previous Formatting & Data
Sheets("Test".Select
Rows("32:32".Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Delete
Columns("A:N".Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.FormatConditions.Delete
Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'===============================Copy from Range in "Form" Worksheet then Paste in "Test"
For i = StartRow To EndRow
Range("RowIndex" = i
Worksheets("Form".Range("B8:N35".Copy 'Destination:=Worksheets("Test".Range("B2"
Sheets("Test".Select
Range("B3".Select
NextRow = Cells(Rows.Count, 2).End(xlUp).Row + 2
Cells(NextRow, 2).Select
ActiveSheet.Paste 'Special Paste:=xlPasteValues
'===============================Copy from Range in "Form" Worksheet then Paste as Values in "Test"
Worksheets("Form".Range("B9:B9".Copy 'Destination:=Worksheets("Test".Range("B2"
Sheets("Test".Activate
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.RowHeight = 37.5
'===============================Reset the Counter to 0 before commencing with Progress Bar
Counter = 0
Counter = Counter + 1
PctDone = Counter / ((EndRow - StartRow) + 1)
'===============================Update Progress Bar and loop through each page
With UserForm1
.FrameProgress.Caption = Format(PctDone, "0%"
.LabelProgress.Width = PctDone * .FrameProgress.Width
End With
' The DoEvents statement is responsible for the form updating
DoEvents
Next i
'===============================Unload the Progress Bar and continue with setting up the page for printing
Unload UserForm1
Range("RowIndex" = StartRow
Range("B2".Value = "=IF(valSelOption=""Q1"",""Quarter 1"",IF(valSelOption=""Q2"",""Quarter 2"",IF(valSelOption=""Q3"",""Quarter 3"",""Quarter 4""))&"" Performance Digest ""&S12&"" - ""&"" ""&Form!J3&"" Theme"""
Range("B2".Select
Selection.RowHeight = 123
If Range("Preview" Then
Dim WorkRange As Range
Dim R As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = Range(Selection, ActiveCell.SpecialCells(xlLastCell))
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For R = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(R)) Then
LASTINROW = WorkRange(R).Value
End If
Next R
'===========================Set Number of Rows Per Page for printing
Dim pages As Integer
Dim pageBegin As String
Dim PrArea As String
Dim p As Integer
Dim q As Integer
Dim nRows As Integer, nPagebreaks As Integer
Dim U As Range
Set U = ActiveSheet.UsedRange
nRows = U.Rows.Count
'===========================add pagebreak every 58 rows
If nRows > 58 Then
nPagebreaks = Int(nRows / 58)
For p = 1 To nPagebreaks
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=U.Cells(58 * p, 1)
Next p
End If
pages = ActiveSheet.HPageBreaks.Count
pageBegin = "$B$2"
For p = 1 To pages
If p > 1 Then pageBegin = ActiveSheet.HPageBreaks(p - 1).Location.Address
q = ActiveSheet.HPageBreaks(p).Location.Row - 1
'PrArea = pageBegin & ":" & "$N$" & Trim$(Str$(q))
PrArea = "$B$2:$N$" & Trim$(Str$(q))
ActiveSheet.PageSetup.PrintArea = PrArea
ActiveSheet.PageSetup.CenterFooter = Cells(q, 1)
Next p
'===========================Preview the selected pages
ActiveSheet.PrintPreview
Else
'===============================If "Preview" not selected, then continue with normal print
Application.Volatile
Set WorkRange = rngInput.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For R = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(R)) Then
LASTINROW = WorkRange(R).Value
End If
Next R
'===========================Set Number of Rows Per Page for printing
Set U = ActiveSheet.UsedRange
nRows = U.Rows.Count
'===========================add pagebreak every 58 rows
If nRows > 58 Then
nPagebreaks = Int(nRows / 58)
For p = 1 To nPagebreaks
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=U.Cells(58 * p + 1, 1)
Next p
End If
pages = ActiveSheet.HPageBreaks.Count
pageBegin = "$B$2"
For p = 1 To pages
If p > 1 Then pageBegin = ActiveSheet.HPageBreaks(p - 1).Location.Address
q = ActiveSheet.HPageBreaks(p).Location.Row - 1
PrArea = pageBegin & ":" & "$N$" & Trim$(Str$(q))
ActiveSheet.PageSetup.PrintArea = PrArea
ActiveSheet.PageSetup.CenterFooter = Cells(q, 1)
Next p
'===========================
Set MySheet = ActiveSheet
MySheet.Range("B:N".PrintOut Copies:=1, preview:=False, ActivePrinter:="Adobe PDF", printtofile:=True, Collate:=True, prtofilename:=PDFFileName
End If
Range("RowIndex" = StartRow
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Once again sorry for the long post, if the code above could be streamlined in any way that would be much appreciated.
Thanks very much in advance for any assistance.
Alex
Apologies in advance for the lengthy post, my code is quite long.
Could someone please take a look at the code below for me and firstly, see if they can fix a couple of things that aren't quite working the way i want them to, and secondly, see if the code can be streamlined in any way as although I have it working in a fashion, I'm not sure if it is the most efficient way of coding this (I'm relatively new to VBA - sorry).
Firsttly, what I am trying to achieve is copy a range from one worksheet to another (this is successful) then I am trying to set the print range automatically and printout the results restricted to the used data range and also setting the page-breaks to specified number of rows (this I can do... kind of) and this is the part I need help with. Basically i want the code below to set page-breaks every 58 rows and this is fine, however, for some reason, the first page is only 56 rows and it's throwing the rest of the pages out.
Secondly, my progress bar was working fine at one stage and now it seems to have malfunctioned, it progresses to teh first iteration bu then stops. So for example, if I have 6 pages printing, it will start off at 0%, then progress to 17% and then stop - the code still completes but there is no visual indication of this to the user.
Sub Main()
Dim Counter As Integer
Dim PctDone As Single
Dim StartRow As Integer
Dim EndRow As Integer
Dim Msg As String
Dim i As Integer
Dim PDFFileName As String
Dim MySheet As Worksheet
If StartRow > EndRow Then
Msg = "ERROR" & vbCrLf & "The starting row must be less than the ending row!"
MsgBox Msg, vbCritical, APPNAME
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Form".Activate
StartRow = Range("StartRow"
EndRow = Range("EndRow"
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
'===============================Clear Previous Formatting & Data
Sheets("Test".Select
Rows("32:32".Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Delete
Columns("A:N".Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.FormatConditions.Delete
Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'===============================Copy from Range in "Form" Worksheet then Paste in "Test"
For i = StartRow To EndRow
Range("RowIndex" = i
Worksheets("Form".Range("B8:N35".Copy 'Destination:=Worksheets("Test".Range("B2"
Sheets("Test".Select
Range("B3".Select
NextRow = Cells(Rows.Count, 2).End(xlUp).Row + 2
Cells(NextRow, 2).Select
ActiveSheet.Paste 'Special Paste:=xlPasteValues
'===============================Copy from Range in "Form" Worksheet then Paste as Values in "Test"
Worksheets("Form".Range("B9:B9".Copy 'Destination:=Worksheets("Test".Range("B2"
Sheets("Test".Activate
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.RowHeight = 37.5
'===============================Reset the Counter to 0 before commencing with Progress Bar
Counter = 0
Counter = Counter + 1
PctDone = Counter / ((EndRow - StartRow) + 1)
'===============================Update Progress Bar and loop through each page
With UserForm1
.FrameProgress.Caption = Format(PctDone, "0%"
.LabelProgress.Width = PctDone * .FrameProgress.Width
End With
' The DoEvents statement is responsible for the form updating
DoEvents
Next i
'===============================Unload the Progress Bar and continue with setting up the page for printing
Unload UserForm1
Range("RowIndex" = StartRow
Range("B2".Value = "=IF(valSelOption=""Q1"",""Quarter 1"",IF(valSelOption=""Q2"",""Quarter 2"",IF(valSelOption=""Q3"",""Quarter 3"",""Quarter 4""))&"" Performance Digest ""&S12&"" - ""&"" ""&Form!J3&"" Theme"""
Range("B2".Select
Selection.RowHeight = 123
If Range("Preview" Then
Dim WorkRange As Range
Dim R As Integer, CellCount As Integer
Application.Volatile
Set WorkRange = Range(Selection, ActiveCell.SpecialCells(xlLastCell))
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For R = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(R)) Then
LASTINROW = WorkRange(R).Value
End If
Next R
'===========================Set Number of Rows Per Page for printing
Dim pages As Integer
Dim pageBegin As String
Dim PrArea As String
Dim p As Integer
Dim q As Integer
Dim nRows As Integer, nPagebreaks As Integer
Dim U As Range
Set U = ActiveSheet.UsedRange
nRows = U.Rows.Count
'===========================add pagebreak every 58 rows
If nRows > 58 Then
nPagebreaks = Int(nRows / 58)
For p = 1 To nPagebreaks
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=U.Cells(58 * p, 1)
Next p
End If
pages = ActiveSheet.HPageBreaks.Count
pageBegin = "$B$2"
For p = 1 To pages
If p > 1 Then pageBegin = ActiveSheet.HPageBreaks(p - 1).Location.Address
q = ActiveSheet.HPageBreaks(p).Location.Row - 1
'PrArea = pageBegin & ":" & "$N$" & Trim$(Str$(q))
PrArea = "$B$2:$N$" & Trim$(Str$(q))
ActiveSheet.PageSetup.PrintArea = PrArea
ActiveSheet.PageSetup.CenterFooter = Cells(q, 1)
Next p
'===========================Preview the selected pages
ActiveSheet.PrintPreview
Else
'===============================If "Preview" not selected, then continue with normal print
Application.Volatile
Set WorkRange = rngInput.Rows(1).EntireRow
Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
CellCount = WorkRange.Count
For R = CellCount To 1 Step -1
If Not IsEmpty(WorkRange(R)) Then
LASTINROW = WorkRange(R).Value
End If
Next R
'===========================Set Number of Rows Per Page for printing
Set U = ActiveSheet.UsedRange
nRows = U.Rows.Count
'===========================add pagebreak every 58 rows
If nRows > 58 Then
nPagebreaks = Int(nRows / 58)
For p = 1 To nPagebreaks
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=U.Cells(58 * p + 1, 1)
Next p
End If
pages = ActiveSheet.HPageBreaks.Count
pageBegin = "$B$2"
For p = 1 To pages
If p > 1 Then pageBegin = ActiveSheet.HPageBreaks(p - 1).Location.Address
q = ActiveSheet.HPageBreaks(p).Location.Row - 1
PrArea = pageBegin & ":" & "$N$" & Trim$(Str$(q))
ActiveSheet.PageSetup.PrintArea = PrArea
ActiveSheet.PageSetup.CenterFooter = Cells(q, 1)
Next p
'===========================
Set MySheet = ActiveSheet
MySheet.Range("B:N".PrintOut Copies:=1, preview:=False, ActivePrinter:="Adobe PDF", printtofile:=True, Collate:=True, prtofilename:=PDFFileName
End If
Range("RowIndex" = StartRow
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Once again sorry for the long post, if the code above could be streamlined in any way that would be much appreciated.
Thanks very much in advance for any assistance.
Alex