Consulting

Results 1 to 11 of 11

Thread: need a new print funtion

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location

    need a new print funtion

    I'm trying to work out a code the will print only pages that have things entered in them. Something like:

    on sheet 1
    if d2 = "" then skip
    else print page 1

    id d55 = "" then skip
    else print page 2

    and so on

    I've been trying to use "if" and "then" funtions to do it, but I can't get them to work in vba. Anyone have any better ideas, or an example to get me started?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Prints()
    Dim sh As Worksheet
    For Each sh In Sheets
    Select Case sh.Index
    Case 1
    If Not sh.Range("D2") = "" Then sh.PrintOut
    Case 2
    If Not sh.Range("D5") = "" Then sh.PrintOut
    Case 3
    If Not sh.Range("A1") = "" Then sh.PrintOut
    End Select
    Next
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location
    How do I get that code to work off of a command button on a different sheet?

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Private Sub CommandButton1_Click()
    Prints
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location
    Thats not workin, all I get is a compile error.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    What type of button are you adding? Forms or ActiveX
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location
    Forms I think.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Assign the macro to the button
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location
    That still isn't working at all. I have it set up like:

    Sheet1 is a inspection report and has the "print" button on it.
    Sheet1 always gets printed.

    Sheet2 is the device list and is 16 pages long. We don't always use all 16 pages. Sometimes we only use 1 of them so I don't want it to print all 16 every time.

    I need a code that will, on printbutton click(), print sheet1 and print only the pages of sheet2 that have been filled out.

    I can modify sheet2 if need be. But I have to keep this thing user friendly and idiot proof.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This assumes regular location of data.

    [VBA]
    Private Sub CommandButton1_Click()
    Dim sh As Worksheet
    Dim i As Long, x As Long
    Sheets(1).PrintOut

    With Sheets(2)
    For i = 2 To 800 Step 50
    x = x + 1
    If .Cells(i, 4) <> "" Then
    .PrintOut from:=x, to:=x
    End If
    Next
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Regular
    Joined
    Jul 2008
    Posts
    43
    Location
    That code was perfect. Not really sure how "x" works, but that was exactly what I needed. Thanks!!!

Posting Permissions

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