+ Reply to Thread
Results 1 to 13 of 13

VBA Code for Print exclude not working

Hybrid View

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    VBA Code for Print exclude not working

    Hi All

    I have the following codes for a print exclude that does not work - it should but prints out all sheets. Any Ideas Please.

    Sub don’t_print_sheet()
        Dim sht
        Dim ShtName
        ShtName = Array("Sheet1", "Sheet2", "Sheet3") 'ADD SHEET NAMES HERE NOT TO PRINT
    For i = 1 To Worksheets.Count
        For J = 0 To UBound(ShtName)
            If Worksheets(i).Name = ShtName(J) Then Count = 1
        Next J
        'If Count = 0 Then Worksheets(i).PrintOut copies:=1
        If Count = 0 Then Worksheets(i).PrintPreview
        Count = 0
    Next i
    End Sub
    Sub Print()
    Unload Me
    Dim ws As Worksheet
    
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> "Database" And _
                ws.Name <> "Master" And _
                ws.Name <> "Dropdown" Then
                ws.PrintPreview
            End If
        Next ws
    
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    Maybe:

    Sub don’t_print_sheet()
        Dim sht
        Dim ShtName
        ShtName = Array("Sheet1", "Sheet2", "Sheet3") 'ADD SHEET NAMES HERE NOT TO PRINT
    For i = 1 To Worksheets.count
        For j = 0 To UBound(ShtName)
            If Worksheets(i).Name = ShtName(j) Then
            count = 1: Exit For: End If
        Next j
        'If Count = 0 Then Worksheets(i).PrintOut copies:=1
        If count = 0 Then Worksheets(i).PrintPreview
        count = 0
    Next i
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    Hi xladept

    Thanks for input....Unfortunately still prints all worksheets

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    Hi Xladept

    Have generated 2 extra worksheets as Linka & Jenny...These are the to that I need to print via Userform "Print Payslips" button.
    Attached Files Attached Files

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    Hi xladept

    I just noticed it is doing the same for populating of worksheets. It is also populating my exclusions Sheet 1-3.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    Hi sintek,

    It ran like a charm!

    Sub don’t_print_sheet()
        Dim Count As Long, i As Long, j As Long
        Dim ShtName
        ShtName = Array("DATABASE", "DROPDOWN", "MASTER") 'ADD SHEET NAMES HERE NOT TO PRINT
    For i = 1 To Worksheets.Count
        For j = 0 To UBound(ShtName)
            If Worksheets(i).Name = ShtName(j) Then
            Count = 1: Exit For: End If
        Next j
        'If Count = 0 Then Worksheets(i).PrintOut copies:=1
        If Count = 0 Then Worksheets(i).PrintPreview
        Count = 0
    Next i
    End Sub

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    Hi xladept

    On my side, all the sheets are printed....(printpreviewed) and all my sheets are also populated by another macro where they should not be.....Am I doing something wrong as a whole cause the :

     If ws.Name <> "Database" And _
                ws.Name <> "Master" And _
                ws.Name <> "Dropdown" Then
    is not working as a whole.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    Hi sintek,

    The sheet names have to match exactly: DATABASE will not Match Database etc.

    So, by comparing a lower case sheet name with database we can get our match.

    Hope that's enough to explain. If not I'll try too be more explicit

    Orrin

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    Now I understand...Awesome thanks for all input.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    I see that the ws.name is case sensitive and in the sample you sent they are all upper case but your other program is proper case - try:

    Sub Print()
    Unload Me
    Dim ws As Worksheet
    
        For Each ws In ActiveWorkbook.Worksheets
            If LCase(ws.Name) <> "database" And _
                LCase(ws.Name) <> "master" And _
                LCase(ws.Name) <> "dropdown" Then
                ws.PrintPreview
            End If
        Next ws
    
    End Sub

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA Code for Print exclude not working

    xladept....Works perfectly. Thanks

    I don't understand though.

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Code for Print exclude not working

    Glad you got it! And, thanks for the rep!

+ 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] Print Macro with exclude range
    By BelshazzarSP in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-20-2016, 01:01 AM
  2. [SOLVED] Private sub code no longer working (print area macro)
    By rikkyshh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2013, 10:43 AM
  3. [SOLVED] seeking formula to exclude non working hours
    By jdgreen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-14-2013, 05:19 PM
  4. Set Print Area Macro - Exclude Blank Rows
    By shudder in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-27-2009, 10:31 PM
  5. Code to print columns with active rows not working
    By debbiesh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-11-2009, 05:14 PM
  6. Print to exclude fill colors on worksheets
    By jman0707 in forum Excel General
    Replies: 3
    Last Post: 10-07-2008, 12:50 PM
  7. Print Code not working
    By Sheeny in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2006, 10:30 AM

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