Ciao Giuseppe,
'-------------------
Scusa Norman. Ancora un'ultima cosa.
E se volessi combinare le due Macro? Cioè se volessi avere la macro
di "John Walkenbach" che permette di selezionare i fogli attivi da
stampare combinata con la parte della tua macro che mi permette di
scegliere la stampante. Quale sarebbe in questo caso il codice
'-------------------
Allora, Giuseppe, suggerirei due soluzioni alternative.
Nella prima soluzione, aggiungo una terza macro assegnata ad un pulsante
nella barra degli strumenti. Secondo la risposta dell'utente, sia la macro
di John Walkenbach che la macro di Norman sarà eseguita:
'=============>>
Public Sub NormanOrJohn()
Dim Res As VbMsgBoxResult
Res = MsgBox(Prompt:="Vuoi SCEGLIERE i fogli da stampare?" _
& vbNewLine & vbNewLine _
& "Se rispondi di no, i fogli " _
& "predeterminati saranno stampati", _
Buttons:=vbYesNo, _
Title:="STAMPARE")
If Res = vbYes Then
Call SelectSheets
Else
Call PrintMySheets
End If
End Sub
'---------------->>
Public Sub SelectSheets()
'John Walkenbach / Aaron Blood
Dim i As Long
Dim TopPos As Long
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim cb As CheckBox
Dim Sheetcount As Long '<<=== NUOVA dichiarazione!
Application.ScreenUpdating = False
' Check for protected workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox "Workbook is protected.", vbCritical
Exit Sub
End If
' Add a temporary dialog sheet
Set CurrentSheet = ActiveSheet
Set PrintDlg = ActiveWorkbook.DialogSheets.Add
Sheetcount = 0
' Add the checkboxes
TopPos = 40
For i = 1 To ActiveWorkbook.Worksheets.Count
Set CurrentSheet = ActiveWorkbook.Worksheets(i)
' Skip empty sheets and hidden sheets
If Application.CountA(CurrentSheet.Cells) <> 0 And _
CurrentSheet.Visible Then
Sheetcount = Sheetcount + 1
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(Sheetcount).Text = _
CurrentSheet.Name
TopPos = TopPos + 13
End If
Next i
' Move the OK and Cancel buttons
PrintDlg.Buttons.Left = 240
' Set dialog height, width, and caption
With PrintDlg.DialogFrame
.Height = Application.Max _
(68, PrintDlg.DialogFrame.Top + TopPos - 34)
.Width = 230
.Caption = "Seleziona Fogli da stampare"
End With
' Change tab order of OK and Cancel buttons
' so the 1st option button will have the focus
PrintDlg.Buttons("Button 2").BringToFront
PrintDlg.Buttons("Button 3").BringToFront
' Display the dialog box
CurrentSheet.Activate
Application.ScreenUpdating = True
If Sheetcount <> 0 Then
If PrintDlg.Show Then
For Each cb In PrintDlg.CheckBoxes
If cb.Value = xlOn Then
Worksheets(cb.Caption).Select Replace:=False
End If
Next cb
ActiveWindow.SelectedSheets.PrintPreview 'Out copies:=1
ActiveSheet.Select
End If
Else
MsgBox "All worksheets are empty."
End If
' Delete temporary dialog sheet (without a warning)
Application.DisplayAlerts = False
PrintDlg.Delete
' Reactivate original sheet
CurrentSheet.Activate
End Sub
'---------------->>
Public Sub PrintMySheets()
'Norman Jones
Dim WB As Workbook
Dim SH As Worksheet
Dim rng As Range
Dim rCell As Range
Dim arrSheets As Variant
Set WB = ActiveWorkbook
Set rng = Selection
Set rCell = ActiveCell
arrSheets = Array("Foglio1", "Foglio3", _
"Foglio5"', "Fogio99") '<<=== da CAMBIARE
For Each SH In Sheets(arrSheets)
With SH.PageSetup
.CenterHeader = "Pagina &P of &N" '<<=== CAMBIATA
End With
Next SH
Application.Dialogs(xlDialogPrinterSetup).Show
Sheets(arrSheets).PrintOut copies:=1
For Each SH In Sheets(arrSheets)
With SH.PageSetup
.CenterHeader = ""
End With
Next SH
ActiveSheet.Select
Application.Goto rng
rCell.Activate
End Sub
'<<=============
Per la seconda soluzione, ho adattato la macro di John Walkenbach per:
- permettere la selezione della stampante
- permettere la numerazione successiva delle pagine stampate
- permettere la selezione di un gruppo predeterminato dei fogli
'=============>>
Public Sub SelectSheets2()
'John Walkenbach / Norman Jones
Dim i As Long
Dim TopPos As Long
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim cb As CheckBox
Dim Sheetcount As Long '<<== Nuova dichiarazione!
Dim PreSelezionati As CheckBox '<<=== Nuova variabile
Dim arrSheets As Variant '<<=== Nuova variabile
Dim SH As Worksheet '<<=== Nuova variabile
Application.ScreenUpdating = False
'Nuova Riga
arrSheets = Array("Foglio1", "Foglio3", _
"Foglio5"', "Fogio99") '<<== da cambiare
' Check for protected workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox "Workbook is protected.", vbCritical
Exit Sub
End If
' Add a temporary dialog sheet
Set CurrentSheet = ActiveSheet
Set PrintDlg = ActiveWorkbook.DialogSheets.Add
Sheetcount = 0
' Add the checkboxes
TopPos = 40
For i = 1 To ActiveWorkbook.Worksheets.Count
Set CurrentSheet = ActiveWorkbook.Worksheets(i)
' Skip empty sheets and hidden sheets
If Application.CountA(CurrentSheet.Cells) <> 0 And _
CurrentSheet.Visible Then
Sheetcount = Sheetcount + 1
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(Sheetcount).Text = _
CurrentSheet.Name
TopPos = TopPos + 13
End If
Next i
'Creare un nuovo controllo per velocemente '<<== Nuova Riga
' selezionare i fogli predeterminati
Set PreSelezionati = PrintDlg.CheckBoxes. _
Add(78, TopPos, 150, 16.5) '<<== Nuova Riga
PreSelezionati.Text = "PreScelti" '<<== Nuova Riga
TopPos = TopPos + 13 '<<== Nuova Riga
' Move the OK and Cancel buttons
PrintDlg.Buttons.Left = 240
' Set dialog height, width, and caption
With PrintDlg.DialogFrame
.Height = Application.Max _
(68, PrintDlg.DialogFrame.Top + TopPos - 34)
.Width = 230
.Caption = "Seleziona Fogli da stampare"
End With
' Change tab order of OK and Cancel buttons
' so the 1st option button will have the focus
PrintDlg.Buttons("Button 2").BringToFront
PrintDlg.Buttons("Button 3").BringToFront
' Display the dialog box
CurrentSheet.Activate
Application.ScreenUpdating = True
If Sheetcount <> 0 Then
If PrintDlg.Show Then
If PreSelezionati.Value = xlOn Then
Sheets(arrSheets).Select '<<== Nuova Riga
Else '<<==
Nuova Riga
For Each cb In PrintDlg.CheckBoxes
If cb.Value = xlOn Then
Worksheets(cb.Caption).Select Replace:=False
End If
Next cb
End If '<<== Nuova
Riga
For Each SH In ActiveWindow. _
SelectedSheets '<<== Nuova Riga
With SH.PageSetup '<<== Nuova Riga
.CenterHeader = _
"Pagina &P of &N" '<<== Nuova Riga
End With '<<== Nuova
Riga
Next SH '<<== Nuova
Riga
Application.Dialogs(xlDialogPrinterSetup).Show
ActiveWindow.SelectedSheets.PrintOut copies:=1
For Each SH In ActiveWindow. _
SelectedSheets '<<== Nuova Riga
With SH.PageSetup '<<== Nuova Riga
.CenterHeader = "" '<<== Nuova Riga
End With '<<== Nuova
Riga
Next SH '<<== Nuova
Riga
ActiveSheet.Select
End If
Else
MsgBox "All worksheets are empty."
End If
' Delete temporary dialog sheet (without a warning)
Application.DisplayAlerts = False
PrintDlg.Delete
' Reactivate original sheet
CurrentSheet.Activate
End Sub
'<<=============
---
Regards,
Norman