VBA Code for printing a data range

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I need some help with code to print rows on a worksheet based on the dates
listed in Column A. I would like a macro that has an input box that would
print the rows between the dates entered in the input box. Example: "Please
enter a beginning and ending date for the range you wish to print"

I did a workaround for this with Advance Filter macro for awhile with the
filtered data being sent to another worksheet, but that was clumsy

if I wanted data from several days.



I would appreciate any help, Steve
 
dim rng as Range
Dim start as String
Dim end as String
Dim res as variant, res1 as variant
start = Inputbox("Enter date for start")
end = Inputbox("enter date for end")
set rng = Range("A1:A365")
if isdate(start) and isdate(end) then
res = application.match(clng(cdate(start)),rng,0)
res1 = application.Match(clng(cdate(end)),rng,0)
if not iserror(res) and not iserror(res1) then
Range(rng(res),rng(res1)).Resize(1,5).Printout
end if
endif


the above is untested but should give you a start.
 
Thanks Tom, I had to change the word "end" to "last" in the code to get it
to accept those lines. .

The modified code below only prints the first row of the "start" date
entered in the start InputBox , (even if the
date entered has many rows of data) and nothing prints from the entry in the
"last" input box.
Hope I didn't cause the problem ! Also, how would I get the headings in
row 1 to print on the
printout too ?

Steve

Sub Print_date()
Dim rng As Range
Dim start As String
Dim last As String
Dim res As Variant, res1 As Variant
start = InputBox("Please enter date for START of print range in MM/DD/YY
format")
last = InputBox("Please enter date for END of print range in MM/DD/YY
format")
Set rng = Range("A1:A365")
If IsDate(start) And IsDate(last) Then
res = Application.Match(CLng(CDate(start)), rng, 0)
res1 = Application.Match(CLng(CDate(last)), rng, 0)
If Not IsError(res) And Not IsError(res1) Then
Range(rng(res), rng(res1)).Resize(1, 5).PrintOut
End If
End If

End Sub

--
 
Change this line
Range(rng(res), rng(res1)).Resize(1, 5).Printout

to

Range(rng(res), rng(res1)).Resize(, 5).Printout

the one resized it to 1 row. My mental error - sorry.

Tested and it works for me. Note the 5 means to print 5 columns - change
to fit your requirements.
 
Thank You Tom. !

--
Steve
Tom Ogilvy said:
Change this line
Range(rng(res), rng(res1)).Resize(1, 5).Printout

to

Range(rng(res), rng(res1)).Resize(, 5).Printout

the one resized it to 1 row. My mental error - sorry.

Tested and it works for me. Note the 5 means to print 5 columns - change
to fit your requirements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top