if and printing options

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » IF and Printing Options

IF and Printing Options

resolvedResolved · Medium Priority · Version 2010

Matthew has attended:
Excel VBA Intro Intermediate course

IF and Printing Options

I am working on a workbook that has multiple version of the same sheet that the user can hide and unhide that sheets as a when they require.

I have included a before print macro which brings up form so the user can choose which report they want to print for relevant department.

My issue is I have been trying to get it when that a certain report is selected on form to be printed it only prints the visible worksheets within the array.

See below the code I am trying to get to work

Private Sub CommandButton5_Click()

If Sheets("Draft Phase 3").Visble = True Then
Sheets(Array("Draft Phase 1", "Draft Phase 2", "Draft Phase 3", "Buildcost Phase 1", "Buildcost Phase 2", "Buildcost phase 3)).Select
Application.Dialogs(xlDialogPrint).Show
PrintOptions.Hide
Sheets("Sheet1").Select
Else
Sheets("Draft Phase 3").Visble = False
Sheets(Array("Draft Phase 1", "Draft Phase 2", "Buildcost Phase 1", "Buildcost Phase 2")).Select
Application.Dialogs(xlDialogPrint).Show
PrintOptions.Hide
Sheets("Draft Phase 1").Select
Else
Sheets("Draft Phase 2").Visble = False
Sheets(Array("Draft Phase 1", "Buildcost Phase 1")).Select
Application.Dialogs(xlDialogPrint).Show
PrintOptions.Hide
Sheets("Draft Phase 1").Select
End If

End Sub

Thank you in advance for the help.

Matt

RE: IF and Printing Options

Hi Matthew,

Thank you for the forum question.

You cannot print hidden sheets.

You will need to unhide the sheets before you can print them.

I have written a code I hope will bring you in the right direction.

Sub PrintSheets()
Dim shSheet As Variant
Dim iCount As Integer
Dim sh As Variant
Dim InvisibleSheets() As String 'array to store the name of hidden sheets
Dim iNumInvSheets As Integer
Dim i As Integer


For Each sh In Sheets
If sh.Visible Then iCount = iCount + 1
Next

iNumInvSheets = Sheets.Count - iCount
ReDim InvisibleSheets(1 To iNumInvSheets) 'from lines above we get the size of the array

For i = 1 To Sheets.Count


If Sheets(i).Visible = xlSheetHidden Then
InvisibleSheets(i) = Sheets(i).Name 'add the names of hidden sheets in the array

End If


Next i


For Each shSheet In Worksheets
If shSheet.Visible = False Then

With shSheet
.Visible = True


' your code to print



End With


End If
Next shSheet

For i = LBound(InvisibleSheets, 1) To UBound(InvisibleSheets, 1)
Sheets(InvisibleSheets(i)).Visible = False 'hide the sheets again
Next i






End Sub




Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: IF and Printing Options

Jens,

Thank you for the response. So this piece of code will unhide and print the hidden sheets as well?

I was trying to get the code to ignore the hidden tabs within the array to print to minimise paper waste ect.

Some schemes the sheet will be used for will range from will 1-10 phases. for example it the scheme is on 2 phase the report will only be about 20 pages. If the print option unhides the hidden tabs every time there will be 80 waste pages.

Regards,

Matt

RE: IF and Printing Options

Hi Matt,

I am sorry.

I haven't read your questions well.

I will be back with another code soon.



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: IF and Printing Options

Hi Matthew,

I can understand that you have problems getting this right. I have tried many options, but cannot get it right.

I will discuss the question with another VBA trainer Monday and come back to you.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: IF and Printing Options

Jens,

Thank you for the update and have a good weekend

Matt

RE: IF and Printing Options

Hi Matthew,

Please have a look at the code below. this will only select visible sheets listed in a Array function. I had to pass the sheet names to a function IsInArray to get it right.

I hope this logic can get your code right.



Sub SelectSheets()
Dim myArray() As Variant
Dim i As Integer
Dim j As Integer
j = 0
For i = 1 To Sheets.Count
If Sheets(i).Visible = True And IsInArray(Sheets(i).Name, Array("Sheet1", "Sheet2", "Sheet3")) Then
ReDim Preserve myArray(j)
myArray(j) = Sheets(i).Name
j = j + 1
End If
Next i
Sheets(myArray).Select
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant)
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))


End Function





Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: IF and Printing Options

Jens,

Thank you for this the code works and ignores the hidden cells but it will only print the current worksheet and not the array.

Regards,

Matt

RE: IF and Printing Options

Hi Matthew,

Please have a look at the code below.

I cannot test it without having the workbook.

It is working in my workbook.

See link below for more print options.

http://www.excelhowto.com/macros/print-worksheets-vba/


Private Sub CommandButton5_Click()
Dim myArray() As Variant
Dim i As Integer
Dim j As Integer

If Sheets("Draft Phase 3").Visble = True Then
j = 0
For i = 1 To Sheets.Count
If Sheets(i).Visible = True And IsInArray(Sheets(i).Name, Array("Draft Phase 1", "Draft Phase 2", "Draft Phase 3", "Buildcost Phase 1", "Buildcost Phase 2", "Buildcost phase 3")) Then
ReDim Preserve myArray(j)
myArray(j) = Sheets(i).Name
j = j + 1
End If
Next i
Sheets(myArray).PrintOut

Sheets("Sheet1").Select
ElseIf Sheets("Draft Phase 3").Visble = False Then
j = 0
For i = 1 To Sheets.Count
If Sheets(i).Visible = True And IsInArray(Sheets(i).Name, Array("Draft Phase 1", "Draft Phase 2", "Buildcost Phase 1", "Buildcost Phase 2")) Then
ReDim Preserve myArray(j)
myArray(j) = Sheets(i).Name
j = j + 1
End If
Next i
Sheets(myArray).PrintOut


Sheets("Draft Phase 1").Select
ElseIf Sheets("Draft Phase 2").Visble = False Then

j = 0
For i = 1 To Sheets.Count
If Sheets(i).Visible = True And IsInArray(Sheets(i).Name, Array("Draft Phase 1", "Buildcost Phase 1")) Then
ReDim Preserve myArray(j)
myArray(j) = Sheets(i).Name
j = j + 1
End If
Next i
Sheets(myArray).PrintOut



Sheets("Draft Phase 1").Select


End If

End Sub




Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Mon 29 Jan 2018: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Keyboard Shortcuts to Add Rows or Columns

Couple of other keyboard shortcuts. Shift+spacebar selects a row, Ctrl+spacebar selects a column. Select either row or column (or several) and use Ctrl and + to insert or Ctrl and - to delete rows or columns.

View all Excel hints and tips


Server loaded in 0.09 secs.