VBA Macro to print table rows from a start date to an end date.

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
180
Office Version
  1. 2021
Platform
  1. Windows
I have a table of records. Column B is the DATE. I have a macro that will print the last 45 rows of the table, but now I need a macro that will print a range of rows by date.
I would be good if it prompted me to enter the dates. For example, Print from Start Date: >=1/1/2023 to End Date: <=12/31/2023.
The columns will be column B to column K.

Here is the macro the prints the last 45 rows of the table:

VBA Code:
Sub RegisterPrint()
    Dim MyLastRow As Long, i As Long, n As Long, StartRow As Long

'    Call ShowAllPrinters
    Application.ScreenUpdating = False
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"
    Application.Dialogs(xlDialogPrinterSetup).Show

    ActiveSheet.PageSetup.PrintArea = ""
    MyLastRow = Range("B" & Rows.Count).End(xlUp).Row
    n = 0
    StartRow = 17
    For i = MyLastRow To 1 Step -1
      If Range("B" & i).EntireRow.Hidden = False Then
        n = n + 1
        If n = 45 Then
          StartRow = i
          Exit For
        End If
      End If
    Next
    
    ActiveSheet.PageSetup.PrintArea = Range("B" & StartRow & ":K" & MyLastRow).Address
'    Application.Dialogs(xlDialogPrinterSetup).Show

'Worksheets("Sheet1").Activate
'ActiveSheet.PageSetup.PrintArea = _
'ActiveCell.CurrentRegion.Address

'    Call RegisterHeader
    
   ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True, _
        IgnorePrintAreas:=False
'    ActiveSheet.PageSetup.PrintArea = ""

    Range("B1048576").End(xlUp).Select
    ActiveCell.Offset(1, 0).Select

'    Call Change_Font
'-------------------------------------
    Range("A1048576").End(xlUp).Select
    ActiveCell.Offset(-16, 0).Select
    Application.GoTo ActiveCell, Scroll:=True
    ActiveCell.End(xlDown).Select
    ActiveCell.Offset(1, 1).Select
    
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Please do not mark a post as the solution unless it actually contains the solution.
If you post the actual solution you used, you can mark that post as the solution.
(If you do that, it could also help out other people who are aiming to do something similar and come across this thread in their searches).
 
Upvote 0
Sorry, I don't know the protocol.

I didn't post my solution because I am new to this VBA stuff and I figured someone who knew what they were doing would come up with a better way.
Here's my solution:
VBA Code:
Sub Macro1()

Dim sdate As Date
Dim edate As Date
Dim ctg As String

'Enter Start Date
    sdate = InputBox("Please enter the Start Date.", "Start Date", "1/1/" & Year(Now), XPos:=2880, YPos:=5760)
'Enter End Date
    edate = InputBox("Please enter the End Date.", "End Date", Date, XPos:=2880, YPos:=5760)
'Enter the Category
    ctg = InputBox("Please enter Category.", "Category", "*", XPos:=2880, YPos:=5760)

    ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=2, Criteria1:= _
        ">=" & sdate, Operator:=xlAnd, Criteria2:="<=" & edate
    
    ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=9, Criteria1:=ctg

End Sub
 
Upvote 0
Solution
Your code looks good to me!
:)
 
Upvote 0

Forum statistics

Threads
1,223,757
Messages
6,174,327
Members
452,555
Latest member
colc007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top