+ Reply to Thread
Results 1 to 3 of 3

VBA Not Printing to LastRow

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2016
    Location
    New Jersey
    MS-Off Ver
    2007
    Posts
    3

    VBA Not Printing to LastRow

    Here's an interesting one...

    I have a list of excel files with their path listed in a spreadsheet and I have a macro that prints them all out. It selects all rows based on i=2 to LastRow. For some reason, it stops after Row 13 and gives me the prompt that all files have been printed, which is the code I have at the end of the loop. It should go to Row 29. When I do a select based on last row, all of the data is selected. If I hardcode it to select 2 to 29, it works fine.

    Anyone run into this where the lastrow is being sele

    Sub PrintBills()
    
    Dim ws As Worksheet
    Dim intRow As Long
    Dim intColumn As Long
    Dim Lastrow As Long
    Dim i As Long
    Dim msgReprint As Integer
    Dim msgReprintQr As Integer
        
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    
    msgReprint = MsgBox("Are you reprinting bills? If Yes, make sure you update column D on the Bill Print tab with the bill you need printed.", vbYesNoCancel)
    msgPrintQR = MsgBox("Do you want to print out the QR sheet?", vbYesNo)
    'Print Current Month QR Sheet
    
    Select Case msgPrintQR
    Case 7
    'User clicked No to print QR sheet
        Range("A1").Select
    Case 6
    'User clicked Yes to print QR sheet
        ActiveSheet.PrintOut
    End Select
    
    'Print Bills
    With Worksheets("Bill Print")
    'Use this to select the worksheet that has the bill information
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    'i is the first row of the bills that need printing
    For i = 2 To 29
    
    Select Case msgReprint
    Case 7
    'User selected No for Reprint
        If .Range("B" & i).Value = "N" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
            ActiveWorkbook.Close Saved = True
        ElseIf .Range("B" & i).Value = "E" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            ActiveWorkbook.PrintOut Copies:=1, Collate:=True
            ActiveWorkbook.Close Saved = True
        ElseIf .Range("B" & i).Value = "Y" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            Set ws = Application.ActiveSheet
            intRow = ActiveCell.SpecialCells(xlLastCell).Row
            intColumn = ActiveCell.SpecialCells(xlLastCell).Column
            Cells(intRow, intColumn).Activate
                If ws.HPageBreaks.Count <> 0 Then
                    ws.PrintOut from:=1, To:=1
                    ws.PrintOut from:=ActiveSheet.HPageBreaks.Count + 1, To:=ActiveSheet.HPageBreaks.Count + 1
                    ActiveWorkbook.Close Saved = True
                Else
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
                ActiveWorkbook.Close Saved = True
                End If
        End If
    Case 6
    'User selected Yes for Reprint, will only select bills on Bill Print with a Y in column D
        If .Range("D" & i).Value <> "Y" Then
        ElseIf .Range("B" & i).Value = "N" And .Range("D" & i).Value = "Y" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
            ActiveWorkbook.Close Saved = True
        ElseIf .Range("B" & i).Value = "E" And .Range("D" & i).Value = "Y" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            ActiveWorkbook.PrintOut Copies:=1, Collate:=True
            ActiveWorkbook.Close Saved = True
        ElseIf .Range("B" & i).Value = "Y" And .Range("D" & i).Value = "Y" Then
            Workbooks.Open Filename:=.Range("C" & i).Value
            Set ws = Application.ActiveSheet
            intRow = ActiveCell.SpecialCells(xlLastCell).Row
            intColumn = ActiveCell.SpecialCells(xlLastCell).Column
            Cells(intRow, intColumn).Activate
                If ws.HPageBreaks.Count <> 0 Then
                    ws.PrintOut from:=1, To:=1
                    ws.PrintOut from:=ActiveSheet.HPageBreaks.Count + 1, To:=ActiveSheet.HPageBreaks.Count + 1
                    ActiveWorkbook.Close Saved = True
                Else
                ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
                ActiveWorkbook.Close Saved = True
                End If
    
        End If
       
        
    Case 2
    'User clicked cancel, macro will stop.
    MsgBox "Process has been cancelled."
    Exit Sub
    End Select
    
    Application.Wait (Now + TimeValue("0:00:03"))
    'Pauses the program 3 seconds between each file so the network printer can pick up the file.
    
    Next i
    
    Select Case msgReprint
    Case 7
        MsgBox "Bills have been sent your default printer. Please wait about 10 minutes to ensure all files are transferred."
    Case 6
        MsgBox "Reprints have been sent to your printer."
    End Select
    
    End With
    
    'Message that bills have been sent to the printer.
    
    
    
    End Sub

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,058

    Re: VBA Not Printing to LastRow

    What return you this?

    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    msgbox Lastrow
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    06-17-2016
    Location
    New Jersey
    MS-Off Ver
    2007
    Posts
    3

    Re: VBA Not Printing to LastRow

    When I use that part of a test selection, all of the data is selected correctly.

    Sub testlastrow()
    
    
    Dim Lastrow As Integer
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("A2:L" & Lastrow).Select
    End Sub

+ 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. Lastrow offset?
    By Gary_J in forum Excel General
    Replies: 5
    Last Post: 06-02-2016, 06:29 AM
  2. R1C1 lastRow not lastRow when data combined
    By Spyderwoman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2016, 05:20 PM
  3. [SOLVED] Using range with lastrow
    By luizmachado in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-22-2016, 06:04 PM
  4. [SOLVED] LastRow - for next loops longer than lastrow?
    By flabb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-07-2016, 12:39 PM
  5. [SOLVED] Find the LastRow
    By SIMBAtheCAT in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2014, 11:45 AM
  6. Copy From LastRow to LastRow+7
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2010, 06:56 AM
  7. Dim Lastrow ? Dim LastColumn
    By khalid79m in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2008, 01:43 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