+ Reply to Thread
Results 1 to 2 of 2

Printing with a macro

  1. #1
    Registered User
    Join Date
    01-19-2019
    Location
    England
    MS-Off Ver
    10
    Posts
    1

    Printing with a macro

    I am trying to create a button that will print a page I have in excel.

    I have an exam analysis sheet for my students, at the top of the page you select the student from a drop down list (numbers 1 to 35, each student is assigned one) and the page fills with their results. Currently If I wish to print this I have to do a page at a time. Is there a way I could create a button that would only print 1 to 10 or 1 to 30 depending on how many student I have in my class? Could we also get it to ask which printer?

    Cheers

    David

  2. #2
    Registered User
    Join Date
    02-14-2013
    Location
    wales
    MS-Off Ver
    Excel 2010
    Posts
    64

    Re: Printing with a macro

    Not sure if this is what you are looking for. One macro prints all and the other allows you to select which worksheets from a menu
    [CODE][/Sub Print_All()
    ' Prints the current active workbook
    ActiveWorkbook.PrintOut
    End Sub
    Sub Print_Selection()
    '
    ' Unprotect Entire Workbook
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ws.Unprotect
    Next ws
    '

    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

    ' 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
    '
    ' Protect Entire Workbook
    Dim wss As Worksheet
    For Each wss In ThisWorkbook.Worksheets
    wss.Protect
    Next wss
    '
    End Sub
    CODE]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Printing Macro help
    By QAChaska in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-13-2013, 01:45 PM
  2. [SOLVED] Macro for auto printing. macro printing at same time 17:30 but do not repeat.
    By aburger in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-23-2013, 09:16 AM
  3. [SOLVED] Printing Macro
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-12-2012, 01:03 AM
  4. Disabling Printing ability of a sheet unless printing from a macro
    By cadamhill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2012, 04:41 AM
  5. Macro for printing
    By cyee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2010, 01:37 AM
  6. printing macro
    By rookie37 in forum Excel General
    Replies: 1
    Last Post: 10-08-2010, 04:20 AM
  7. Printing macro
    By ntffl07 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-11-2010, 04:41 PM
  8. Printing Macro-How can I make my print macro force excel
    By chris_mayer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2006, 09:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1