I need to insert the result of my userform into a header but i don't know how to combine my codes into a final project : Photos and code below
i need the header OK Button to
1: Format the header according to my header code depending on what sheet i want, in this case the sheet called metals
2. After it says "Summary of Metals in "_____" <-(Soil/Sediment...etc depending on which box is checked)
3. Insert what ever text is entered into the userform text box. (no code written yet).
The final result. = For this particular sheet would be the header saying "Summary of Metals in Soil, 100 Main Street, USA"
All help is appreciated!
The code below inserts the result into A1 just temporary
Private Sub Cancel_Click()
Me.Hide
End Sub
Private Sub OK_Click()
'--- Insert the correct matrix Wording ---
If Check_Soil.Value = -1 Then
Range("A1").Value = "Soil"
ElseIf Check_Sediment.Value = -1 Then
Range("A1").Value = "Sediment"
ElseIf Check_Ground_Water.Value = -1 Then
Range("A1").Value = "Ground Water"
ElseIf Check_Surface_Water.Value = -1 Then
Range("A1").Value = "Surface Water"
End If
Me.Hide
MsgBox "Completed", vbOKOnly
End Sub
Private Sub Check_Soil_Click()
'--- Checks if the Soil Button is Clicked ---
If Check_Soil.Value = True Then
Check_Surface_Water.Value = False
Check_Ground_Water.Value = False
Check_Sediment.Value = False
Else
Check_Soil.Enabled = True
End If
End Sub
Private Sub Check_Surface_Water_Click()
'--- Checks if the Surface Water Button is Clicked ---
If Check_Surface_Water.Value = True Then
Check_Soil.Value = False
Check_Ground_Water.Value = False
Check_Sediment.Value = False
Else
Check_Surface_Water.Enabled = True
End If
End Sub
Private Sub Check_Ground_Water_Click()
'--- Checks if the Ground Water Button is Clicked ---
If Check_Ground_Water.Value = True Then
Check_Surface_Water.Value = False
Check_Soil.Value = False
Check_Sediment.Value = False
Else
Check_Ground_Water.Enabled = True
End If
End Sub
Private Sub Check_Sediment_Click()
'--- Checks if the Sediment Button is Clicked ---
If Check_Sediment.Value = True Then
Check_Surface_Water.Value = False
Check_Soil.Value = False
Check_Ground_Water.Value = False
Else
Check_Sediment.Enabled = True
End If
End Sub
My OTHER CODE:
SubSelect_Correct_Sheet()
' Select_Correct_Sheet Macro
Sheets("Metals").Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = "&""Arial,Bold""Summary of Metals in "
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
Try these two changes
1- change your OK_Click into this:
Private Sub OK_Click()
Dim headerText As String
Select Case True
Case Check_Soil.value: headerText = "Soil"
Case Check_Sediment.value: headerText = "Sediment"
Case Check_Ground_Water.value: headerText = "Ground Water"
Case Check_Surface_Water.value: headerText = "Surface Water"
End Select
headerText = headerText & ", " & TextBox1.value ' <-- assuming this is the name of your textbox
FormatHeader headerText ' <-- now invoke the header formatting sub with parameter
MsgBox "Completed"
End Sub
2- Change your routine of formatting the header (old name was Select_Correct_Sheet I gave it a new name, FormatHeader). I should have a parameter text in its declaration and only one line will change, the one where the text is assigned in order to add the provided parameter.
Sub FormatHeader(text As String)
' ....
.LeftHeader = "&""Arial,Bold""Summary of Metals in " & text '<-- add the text parameter into header here
' ....
End Sub
Related
I'm trying to get a code which will automatically set the print view mode around a certain range of cells (selected cells - as it has to be dynamic) and remove the page number (PAGE 1 in black superposed on cells).
I have tried the following (below), however it doesn't work as it grabs all the sheet data and puts it on one page instead of just showing the selected range on one page.
Dim myRange As Range
Set myRange = Selection
ActiveWindow.View = xlPageBreakPreview
myRange.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.708661417322835)
.RightMargin = Application.InchesToPoints(0.708661417322835)
.TopMargin = Application.InchesToPoints(0.748031496062992)
.BottomMargin = Application.InchesToPoints(0.748031496062992)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub
So basically I want the selected cells to be highlighted (white) and put only on one page (no page breaks) and anything else in the sheet to be put outside of the print view (in that gray/black view).
Any thoughts would be welcome.
Thank you !
One thing to remember when recording macros is that default settings don't usually need to be mentioned, unless you're changing the figure.
So things like .LeftHeader = "" aren't needed unless you're specifically removing text from the left header - by default it's blank, and this code is just saying keep it blank.
The page number superimposed on the sheet is just because you're in Page Break Preview (your code puts you in that at the start). You only need to look at that view if you're manually changing the print - using code just stay in the Normal view (it's rare you have to Select anything with VBA before working with it).
This code worked for me:
Sub Test()
Dim PrintRange As Range
Set PrintRange = Selection
With PrintRange.Parent.PageSetup 'The Parent of Selection is the sheet.
Application.PrintCommunication = True
.PrintArea = PrintRange.Address
Application.PrintCommunication = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End Sub
I have a chart that grows each day and currently it's spanning A-BC columns.
If I want to print the last page I don't get the Y-axis labels with the print.
How can I print a the last page and include the Y-axis labels so that the chart looks "complete"?
First you need to make sure the A-column has the Y-axis labels and nothing more. Resize the column so that it only have the labels and not the start of the chart.
Then we use VBA by copying the A-column and paste it as an image and move it to the print area, print the sheet and remove the image.
Sub print_last_page()
Set ws = ActiveSheet
'copy Y-axis lables, paste as image in B1 and rename the object to "Ylabels"
ws.Range("A1:A72").CopyPicture xlScreen, xlBitmap
ws.Range("B1").PasteSpecial
Selection.ShapeRange.Name = "Ylabels"
' this finds the last column the the chart named "Buffertpallplatser %" occupies
x = ws.Shapes("Buffertpallplatser %").Width
y = 0
With ws.Shapes.AddLine(x, y, x, y)
.TopLeftCell.Select
.Delete
End With
endcol = Selection.Column ' save this column for later use
' do the same thing again but roll back 750 pixels.
' 750 pixels is about what fits the paper with the given heights of my charts, change to suit you needs
x = ws.Shapes("Buffertpallplatser %").Width - 750
y = 0
With ws.Shapes.AddLine(x, y, x, y)
.TopLeftCell.Select
.Delete
End With
posleft = Selection.Left ' save this column also for later use.
ws.Shapes.Range(Array("Ylabels")).Left = posleft ' move the image to the left most position of our print area
ActiveCell.Select ' select the cell again
' resize the selection to the chart area, in my case 73 rows and the 750 pixels width in columns
Selection.Resize(Selection.Rows.Count + 73, endcol - Selection.Column + 1).Select
' Below is a recorded macro to print the selection and scale to fit the columns
' I advice you to record something that suits your case.
Application.PrintCommunication = False
With ws.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ws.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ws.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Selection.PrintOut Copies:=1, Collate:=True
' remove the image from the sheet again.
ws.Shapes.Range(Array("Ylabels")).Delete
End Sub
If we step through the code it will generate this:
And the end result on paper will look like this:
I have been working on this macro for a few days now, and have hit a problem which I haven't been able to solve with my limited VBA knowledge. It works fine for the first two sheets, but after that it seems to be going wrong and not returning the correct ranges from the second workbook (MACRO Customs Invoices) or re-merging the cells in ThisWorkbook, so the end pdf doesn't have the information I want as is formatted all wrong. Please help me, good people! Your help would be hugely appreciated.
Sub VBAexperimentalv6()
Dim invoice As Workbook
Dim invoicews As Worksheet
Dim origlotno As Range
Dim invoiceci As Range
Dim macroci As Range
Dim remlotno As Range
Dim rhci As Range
Set invoice = ThisWorkbook
For Each invoicews In ThisWorkbook.Worksheets
invoicews.Activate
Set origlotno = Range("D15", Range("D15").End(xlDown))
Set invoiceci = Range("F15", Range("F15").End(xlDown))
Set remlotno = invoicews.Range("D15:E15")
Set remlotno = remlotno.Resize(origlotno.Rows.Count)
remlotno.UnMerge 'unmerges lot number column
With origlotno
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("A2").Resize(.Rows.Count, .Columns.Count).Value2 = .Value2 'copies lot numbers from invoice to macro in dynamic ranges
End With
invoicews.Range("D5:K5").UnMerge 'unmerges client name cells
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("M3").Value = invoicews.Range("D5").Value ' copies client name to formula sheet
Set macroci = Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("J2")
Set macroci = macroci.Resize(invoiceci.Rows.Count, invoiceci.Columns.Count)
invoiceci.Value = macroci.Value ' copies customs info into invoice
invoicews.Range("D5:K5").Merge 'merges client name
remlotno.Merge (True) ' merges lot no in variable range
invoicews.Rows("2:2").RowHeight = 60 ' sets address row to correct height
Set rhci = invoicews.Rows("15:15")
Set rhci = rhci.Resize(origlotno.Rows.Count)
rhci.RowHeight = 21 'resizes lot no rows to allow for two lines
Application.PrintCommunication = False
With invoicews.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
invoicews.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With invoicews.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.393700787401575)
.RightMargin = Application.InchesToPoints(0.196850393700787)
.TopMargin = Application.InchesToPoints(0.393700787401575)
.BottomMargin = Application.InchesToPoints(0.393700787401575)
.HeaderMargin = Application.InchesToPoints(0.393700787401575)
.FooterMargin = Application.InchesToPoints(0.393700787401575)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 0
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
invoicews.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Richard\Documents\Sortcoding\Customs Invoices\" & Workbooks("MACRO Customs
Invoices.xlsx").Sheets("Sheet1").Range("M2").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False ' saves as pdf using formula generated file name
Workbooks("MACRO Customs Invoices.xlsx").Sheets("Sheet1").Range("A2:A250").Clear ' clear formula sheet ready for next invoice
Next invoicews
End Sub
I am new to the wonders and world of Macro, VBA and RPA, and would like to study it more. Recently did a short course on RPA.
Just want to share my problem and throw a question out to the Community here.
My Pain Point:
I’m the person printing the Payslips for the Company.
Currently I am opening all 30+ Single Excel File Payslips generated by Excel VBA (not done by me) for my company one by one and setting to print by Set Print Area for EACH Payslip Standalone Excel File, and printing them one by one.
This takes up quite some time which I believe can be saved with either the right Print Settings, VBA or RPA.
Unfortunately I am still exploring these and know nothing about VBA.
Id like to check for VBA, how I can go about macro-ing the process such that I can ease my workflow in the following:
Opening Payslip one by one
Setting the Print Area (same throughout)
Printing
If any of these can be automated it'd save me time and frustration.
Regarding the code it might be a merge of these two
https://www.ablebits.com/office-addins-blog/2019/08/20/set-change-print-area-excel/
https://www.excelhowto.com/macros/print-all-workbooks-in-a-folder/
Anyone can advise step by step what I am to do? I have read and tried but do not understand still.
Tested and working.
This code loops files in a folder, selects A1:K41 and prints the selected range to your standard printer.
Sub run()
FolderPath = "PATH TO FOLDER"
FileNme = Dir(FolderPath & "\*.xlsx") ' returns the first xlsx file in the folder
Do While Len(FileNme) > 0 ' loop while there are files.
Set wb1 = Workbooks.Open(FolderPath & "\" & FileNme) ' open file
wb1.Sheets("Sheet1").Range("A1:K41").Select ' select the range
'below is recorded macro of print selected area
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Selection.PrintOut Copies:=1, Collate:=True
'close payslip
Application.DisplayAlerts = False
wb1.Close
Application.DisplayAlerts = True
FileNme = Dir ' get the next file name
Loop
End Sub
I have the following code which works fine:
Sheets("File1").Select
Range("B2:J56").Select
Selection.PrintOut Copies:=1, Collate:=True
But I need to print several ranges and the pages should comply with the following formats:
every selection should fit an A4 paper
double-sided
columns adjusted in one page so that the content fits entirely in the page width
quality print of 600 dpi
margins centred (horizontally and vertically)
pages should include a header "This data was collected a year ago"
After struggling on the internet to no avail, I have recorded a macro but there are still many errors when I try to reproduce the code snippet,
specially in time execution error #1004 in line ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37" and print communication method failed.
[EDIT]: Solution implemented below
Sub RECORDED_Macro_Print_test()
Application.ScreenUpdating = False
Sheets("File1").Select
Range("B2:J83,L2:T37,V2:AO92").Select
Range("V2").Activate
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$83;$L$2:$T$37;$V$2:$AO$92" ' <----- Fix it by replacing ';' by ',' as suggested by the user 'Siva'
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = "" ' <----- Not necessary
.PrintTitleColumns = "" ' <----- Not necessary
.LeftHeader = "" ' <----- Not necessary
.CenterHeader = "This data was collected a year ago"
.RightHeader = "" ' <----- Not necessary
.LeftFooter = "" ' <----- Not necessary
.CenterFooter = "" ' <----- Not necessary
.RightFooter = "" ' <----- Not necessary
.LeftMargin = Application.InchesToPoints(0.511811023622047)
.RightMargin = Application.InchesToPoints(0.511811023622047)
.TopMargin = Application.InchesToPoints(0.78740157480315)
.BottomMargin = Application.InchesToPoints(0.78740157480315)
.HeaderMargin = Application.InchesToPoints(0.31496062992126)
.FooterMargin = Application.InchesToPoints(0.31496062992126)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = "" ' <----- Not necessary
.EvenPage.CenterHeader.Text = "" ' <----- Not necessary
.EvenPage.RightHeader.Text = "" ' <----- Not necessary
.EvenPage.LeftFooter.Text = "" ' <----- Not necessary
.EvenPage.CenterFooter.Text = "" ' <----- Not necessary
.EvenPage.RightFooter.Text = "" ' <----- Not necessary
.FirstPage.LeftHeader.Text = "" ' <----- Not necessary
.FirstPage.CenterHeader.Text = "" ' <----- Not necessary
.FirstPage.RightHeader.Text = "" ' <----- Not necessary
.FirstPage.LeftFooter.Text = "" ' <----- Not necessary
.FirstPage.CenterFooter.Text = "" ' <----- Not necessary
.FirstPage.RightFooter.Text = "" ' <----- Not necessary
End With
Application.PrintCommunication = True ' <----- Remove this line to fix it. This is an error issue
Selection.PrintOut Copies:=1, Collate:=True
End Sub
To record the macro I have basically selected all the ranges I needed by holding the SHIFT key, then I defined the print area, then pressed print, specified all constraints in the dialog box and voilà. How to get it to work?
Replace your code line
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37"
with
ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56,$L$2:$T$37,$V$2:$AH$37"
I have tried it in my machine and that line didn't give any error after replacing