I have found some codes and modified the same but not getting the desired result.As my knowledge in VBA is limited to copy/paste I need help of seniors. the code is represented below:
Sub SelectPrinterAndSheets()
Dim i As Integer
Dim TopPos As Integer
Dim SheetCount As Integer
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim cb As CheckBox
Application.ScreenUpdating = False
Application.Dialogs(xlDialogPrinterSetup).Show
' 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 = "Select sheets to print"
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).Activate
ActiveSheet.PrintOut
' ActiveSheet.PrintPreview 'for debugging
End If
Next cb
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
The problems & modification reqd. are given below:
1. If you run the code you will first get a menu to select printer. Then whether you press "cancel " or "OK" you will be ahown the seklect sheets to print dialog. I wnat that if the user clicks cancel "select sheets to print" dialog should not appear.
2. "Selesct sheets to print " dialog will take you to last sheet even if you press it from any other sheet say sheet1 or sheet2 etc. I want that the code should not go to l;ast sheet and should stay on the sheet from where the user has run the code.
3. this code does not show hidden sheets . I want that this should show hidden sheets also to print & should actually take print if users select the sheet & click print.(hidden sheets means normally hidden sheets from excel menu Format\sheet\hide) But should not show very hidden sheets done through macro.
4. I want that the dialog should not show one specific sheet whether hidden or not named "extract".
A sample workbook is posted for testing purpose.