Solved

Excel print range

Posted on 2012-04-13
6
233 Views
Last Modified: 2012-07-12
I have a macro that I use to print a range of cells.  If it does not = 0 then it prints.  It worked until I tried the same macro in the Excel 2010 - it works except that it prints all - even if the sum is zero.

Any help is appreciated.

For x = 6 To 19
    If Sheet2.Cells(x, 8).Value <> 0 Then
        y = x - 3
        Sheets(y).Select
        ActiveSheet.PageSetup.PrintArea = "$B$2:$F$21"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1
        End If
   Next x

  For x = 6 To 19
    If Sheet2.Cells(x, 11).Value <> 0 Then
        y = x - 3
        Sheets(y).Select
        ActiveSheet.PageSetup.PrintArea = "$I$2:$M$21"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1
        End If
   Next x
For x = 6 To 19
    If Sheet2.Cells(x, 14).Value <> 0 Then
        y = x - 3
        Sheets(y).Select
        ActiveSheet.PageSetup.PrintArea = "$b$22:$f$41"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1
        End If
   Next x
For x = 26 To 39
    If Sheet2.Cells(x, 14).Value <> 0 Then
        y = x - 23
        Sheets(y).Select
        ActiveSheet.PageSetup.PrintArea = "$I$22:$M$41"
        ActiveWindow.SelectedSheets.PrintOut Copies:=1
        End If
   Next x
End Sub
0
Comment
Question by:JohnMac328
6 Comments
 
LVL 6

Expert Comment

by:HomerTNachoCheese
ID: 37844577
Have you tried inserting a breakpoint at the beginning of your code, then step through the code to check the values line by line?
0
 

Author Comment

by:JohnMac328
ID: 37844583
No - trying to help someone out - not an excel person :)
0
 
LVL 2

Expert Comment

by:mdeweerd
ID: 37845100
Maybe the values are not really 0 and need to be rounded:
Example:
Round(Sheet2.Cells(x, 14).Value, 2)

Open in new window


Adjust ',2' up to the number of digits needed.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37845472
FYI - you can optimize your code from:

    
If Sheet2.Cells(x, 8).Value <> 0 Then
    y = x - 3
    Sheets(y).Select
    ActiveSheet.PageSetup.PrintArea = "$B$2:$F$21"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1
End If

Open in new window


To

    If Sheet2.Cells(x, 8).Value <> 0 Then
        y = x - 3
        With Sheets(y)
            .PageSetup.PrintArea = "$B$2:$F$21"
            .PrintOut copies:=1
        End With
        
    End If

Open in new window


There's no need to select sheets or use the ActiveWindow to set print areas or print sheets.

I just tested your code out with dummy data and it worked fine for me.  I put a 1 in H6,  K6, N6, and N26 and it printed 4 times.

The test is for non-zero so rounding should not be an issue.  Just check those ranges and advise if the value is not zero.  If that doesn't work, then try this and see what it tells you:

You can try this amended code below which creates a LogSheet (assuming the workbook is not protected) and logs what is happening.  Check the log and see what values are being displayed for these ranges.  If you are getting nothing, then the ranges being examined don't have data - or at least they don't have non-zero data, and that's what the if statement is looking for.

Please advise.

Sub test()
Dim wkb As Workbook
Dim wksNew As Worksheet
Dim outCursor As Range

    Set wkb = ThisWorkbook

    On Error Resume Next
    Set wksNew = wkb.Worksheets("Log Sheet")
    If Err.Number <> 0 Then
        Set wksNew = wkb.Worksheets.Add(after:=wkb.Worksheets(wkb.Worksheets.Count))
        wksNew.Name = "Log Sheet"
    End If
    On Error GoTo 0

    wksNew.Cells.Clear
    Set outCursor = wksNew.Range("A1")

    For x = 6 To 19
        outCursor.Value = "BLOCK 1 Sheet2.Cells(" & x & ",8).Address is  = " & Sheet2.Cells(x, 8).Address & " having value: " & Sheet2.Cells(x, 8).Value
        Set outCursor = outCursor.Offset(1, 0)
        If Sheet2.Cells(x, 8).Value <> 0 Then
            y = x - 3
            With Sheets(y)
                .PageSetup.PrintArea = "$B$2:$F$21"
                .PrintOut Copies:=1
            End With
        End If
    Next x

    For x = 6 To 19
        outCursor.Value = "BLOCK 2 Sheet2.Cells(" & x & ",11).Address is  = " & Sheet2.Cells(x, 11).Address & " having value: " & Sheet2.Cells(x, 11).Value
        Set outCursor = outCursor.Offset(1, 0)
        If Sheet2.Cells(x, 11).Value <> 0 Then
            y = x - 3
            With Sheets(y)
                .PageSetup.PrintArea = "$I$2:$M$21"
                .PrintOut Copies:=1
            End With
        End If
    Next x
    For x = 6 To 19
        outCursor.Value = "BLOCK 3 Sheet2.Cells(" & x & ",14).Address is  = " & Sheet2.Cells(x, 14).Address & " having value: " & Sheet2.Cells(x, 14).Value
        Set outCursor = outCursor.Offset(1, 0)
        If Sheet2.Cells(x, 14).Value <> 0 Then
            y = x - 3
            With Sheets(y)
                .PageSetup.PrintArea = "$b$22:$f$41"
                .PrintOut Copies:=1
            End With
        End If
    Next x
    For x = 26 To 39
        outCursor.Value = "BLOCK 4 Sheet2.Cells(" & x & ",14).Address is  = " & Sheet2.Cells(x, 14).Address & " having value: " & Sheet2.Cells(x, 14).Value
        Set outCursor = outCursor.Offset(1, 0)
        If Sheet2.Cells(x, 14).Value <> 0 Then
            y = x - 23
            With Sheets(y)
                .PageSetup.PrintArea = "$I$22:$M$41"
                .PrintOut Copies:=1
            End With
        End If
    Next x


End Sub

Open in new window


Cheers,

Dave
0
 
LVL 2

Expert Comment

by:mdeweerd
ID: 37845832
"The test is for 0 so rounding should not b an issue"
-> I did actually run in this kind of issue in the past where the sum of values looked as if it was zero, but they were not.  In the end these are floating point numbers and rounding will make a near zero a perfect zero.
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37845856
I withdraw my comment, you are absolutely correct. 00000000001 may be zero to excel.

Let's see what the test I submitted provides and hopefully there's some answer there as well.

Cheers,

Dave
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now