Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Cells based on Todays date and print selection 1

Status
Not open for further replies.

liamm

Technical User
Aug 12, 2002
102
GB
Hi on my worksheet I have 6 columns. The first column is a date 'ddd-dd-mm', the next 5 are general text. Each Date cell is a merge of 3 rows (example: A2 covers Rows 2,3,4 and so on). So for example today covers the cells A2:F4.

I would like to create a macro (activated by a button) which (based on todays date) selects the next 7 days and prints only the associated cells. For example if today starts at A2(and covers 3 rows)tomorrow is A5 etc, I want to select cells A2:F22 and Print the selection.

Hope this is clear
Liam

If you can help, GREAT
If I can help, EVEN BETTER
 
Hi,

What work have you done on this taks? Please post the code that you have so far.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Also, if possible can the worksheet be automated so when it opens it places the Row with todays date at the top of the screen?
Liam

If you can help, GREAT
If I can help, EVEN BETTER
 
Hi SkipVought,
all I have managed so far is to focus on Todays date but this only focuses on the cell, it doesn't put that Row at the top of the page.
I also recorded a macro to print a selection of cells but of course this only selcted the same cells each time. I didn't know where to start with moving the cells down each day

Liam

If you can help, GREAT
If I can help, EVEN BETTER
 


Why not use the AutoFilter to select dates >= Today AND < Today + 5

Turn on your macro recorder. Post your code.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi, I tried this and it removed all fields.
The code is:

Sub Macro2()
Selection.AutoFilter Field:=1, Criteria1:=">=TODAY", Operator:=xlAnd, _
Criteria2:="<TODAY+5"
End Sub

Do I have to tell it the date format? and if yes , how would I do this
Liam

If you can help, GREAT
If I can help, EVEN BETTER
 



You have to use TODAY's DATE!

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Can I not use something like Now()? instead of having to type in the date each time

If you can help, GREAT
If I can help, EVEN BETTER
 


Post your recorded code. Variables do not work using Excel features. That's why you are recording code so that we can get it to work with variables like Date or Now.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi, when I use a date it filters:

Sub Macro1()
Selection.AutoFilter Field:=1, Criteria1:=">=18/01/2009", Operator:=xlAnd _
, Criteria2:="<25/01/2009"
End Sub

Can I do this without having to type in the date?

If you can help, GREAT
If I can help, EVEN BETTER
 
Assuming that your table starts in A1...
Code:
Sub Macro1()
    [A1].AutoFilter Field:=1, _
       Criteria1:=">=" & format(date,"dd/mm/yyyy"), Operator:=xlAnd, _
       Criteria2:="<" & format(date+5,"dd/mm/yyyy")"
End Sub

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sorry, this didn't work. I got a compile error showing the last " mark at the end of Criteria 2 line.

When I removed this " the macro ran but the filter showed no fields.

If you can help, GREAT
If I can help, EVEN BETTER
 
Code:
Sub Macro1()
    [A1].AutoFilter Field:=1, _
       Criteria1:=">=" & format(date,"dd/mm/yyyy"), Operator:=xlAnd, _
       Criteria2:="<" & format(date+5,"dd/mm/yyyy")
End Sub
sorry, it had a trailing QUOTE in criteria2.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sorry, this did not work It filtered out all the results so that it showed no data at all.
Can I ask, in the coding how does it get todays date from the code?

Thanks for all this help,
Liam

If you can help, GREAT
If I can help, EVEN BETTER
 


Hmmm???

Works on my sheet. Since my regional settings (mm/dd/yyyy format) are probably different than yours, here's my code...
Code:
Sub Macro11()
    [A1].AutoFilter Field:=1, _
       Criteria1:=">=" & Format(Date, "mm/dd/yyyy"), Operator:=xlAnd, _
       Criteria2:="<" & Format(Date + 4, "mm/dd/yyyy")
End Sub
The current date in VB is Date.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 



Are you using REAL DATES? faq68-5827.

By changing the Cell Format in column A to GENERAL, it should display NUMBERS; for instance today's date serial number is 39831.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi again, tried your last code and it worked - mm/dd/yyyy.
I have formatted my cells ddd-dd-mmm and used your code and the script works smashing.
I just need to get my head around how I want the spreadsheet to work now. i.e. I would like it to Open with todays cell at the top of the page and when I run the code you have created I would like it to print the selection as part of the same code. For example the button could say "Print next 7 days" and the code would filter the data then open the Print info box so the user just needs to select the printer (or even print the sheet automatically)

Liam


If you can help, GREAT
If I can help, EVEN BETTER
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top