Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Print out data based on DATE with exclude the other

0

Hi guys,

I want printing data based on DATE(TODAY), just print out column A,B and column (DATE)  the DATE depends on header .every day will add column contains DATE

so when print out the  data should exclude column C and  the others columns contain old DATE . if I have whole column (DATE) is empty then print out the last column contains numeric values  with some empty cells even the column is not DATE(TODAY).

also should exclude empty rows from print out if the ITEM column doesn't contain number, then will the whole row is empty, shouldn't print out .

Answer
Discuss

Answers

0
Selected Answer

Hello Amer omar and welcome to the forum,

In the attached file I have modified your file to include the following macro which I believe will do what you want. I added a worksheet called "Print Report" which is where your desired data is copied to for printing. On the "Print Report" sheet just click the button.

Sub PrintReport()

' macro written by WillieD24 for teachexcel. com

' 1 > macro will clear existing data from "Print Report" sheet
' 2 > macro copies col "A" & "B" and most recent date column to "Print Report" sheet
' 3 > remove rows from "Print Report" sheet where there is an empty cell in col "A"
' 4 > macro prints report to default printer

' 1 > clear existing data from "Print Report" sheet
Sheets("Print Report").Select
    Cells.Select
    Selection.ClearContents
    Range("A1").Select

' 2 > copy col "A" & "B" and most recent date column to "Print Report" sheet
Sheets("STOCK").Columns("A:B").Copy
    Sheets("Print Report").Paste
    Range("C1").Select
    Application.CutCopyMode = False
' find last column with data on "STOCK" sheet
Sheets("STOCK").Select
Dim LC As Long
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Dim LRowLC As Long

Do Until LRowLC > 1
LRowLC = Cells(Rows.Count, LC).End(xlUp).Row
    If LRowLC = 1 Then LC = LC - 1
Loop
' copy column to "Print Report" sheet - col "C"
Sheets("STOCK").Columns(LC).Copy
    Sheets("Print Report").Paste
    Range("A1").Select
    Application.CutCopyMode = False
    Range("A1").Select

' 3 > remove rows from "Print Report" sheet where there is an empty cell in col "A"
Sheets("Print Report").Select
Range("A1").Select
With Sheets("Print Report")
    Sheets("Print Report").Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With

' 4 > print report to default printer
With Sheets("Print Report")
    .Activate
    .Range("A1").Select
    .PrintOut Copies:=1
End With

End Sub

If this is what you were looking for please mark my answer as Selected.

Cheers   :-)

Discuss

Discussion

Hi,
gives error "variable not defined" in this word SelectedSheets
Amer omar (rep: 2) Mar 2, '24 at 4:38 am
@Amer

Assuming you have only one instance of Excel open, try:
Windows(1).SelectedSheets.PrintOut Copies:=1

If that works, please remember to mark Willie's Answer as Selected

@Willie - I'm mobile at present (so can't test my suggestion) but please check and correct Answer if needed. 


John_Ru
John_Ru (rep: 6142) Mar 2, '24 at 5:39 am
Hi John
it works greatly !
thank you so much for you & Willie.
Amer omar (rep: 2) Mar 2, '24 at 5:48 am
@John
Thanks for the fix. Having more than one file open is something I hadn't considered. I originally had 
ActiveWorkbook.SelectedSheets.PrintOut Copies:=1
but I thought having "ActiveWorkbook" was redundant.
Also, congrats on breaking thru 6K !
Cheers   :-)
WillieD24 (rep: 557) Mar 2, '24 at 5:40 pm
@Amer

Glad that helped. Thanks for your comment and for selecting Willie's Answer.

@Willie

You can't use .SelectedSheets with either ActiveWorkbook or (my preferred) ThisWorkbook since neither has that as a property.

I suggest instead that you end your code as foillows (changes in bold):
' 4 > print report to default printer
With Sheets("Print Report")
    .Activate
    .Range("A1").Select
    .PrintOut Copies:=1
End With
 
End Sub

(which avoids use of .SelectedSheets). I think that should work in all scenarios. If you agree, kindly revise your Answer and file to suit.

Thanks for noticing my passing 6,000 points- you're the only one who commented! I remember when I started to answer questions on the Forum and saw that the legendary Variatus was on 4,000+ points. I never thought I'd even get close to him.
John_Ru (rep: 6142) Mar 3, '24 at 8:12 am
@John,
Thanks for the insight. I have amended my macro, the file, and my post as per your recommendation.

Cheers   :-)
WillieD24 (rep: 557) Mar 3, '24 at 10:18 am
@Willie - thank you sir! 
John_Ru (rep: 6142) Mar 3, '24 at 11:32 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login