Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default macro code to open a file dated yesterday

I need to code a macro that will open a file dated yesterday copy data and
paste it into todays file so I can compare changes from yesterday to today.
I save all the files with a date stamp, so I want to open only the most
recent file, but if it s a Monday, I need it to open Friday's data...any
suggestions?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default macro code to open a file dated yesterday

The first thing that we would need to know is how do you express the date in
the file name? Is it yymmdd, yyyymmdd, mmddyyyy, ddmmyyyy? Personally I
would name them yyyymmdd so that they sort correctly. So assume the file is
called 20070905log.xls

Next where is the data that needs to be copied?

finally where does it need to go so you can compare it? A separate sheet or
a column on an existing sheet? Are the sheets named?

Answering these questions will get you on the way to a solutions. Try
recording what you want to do using the macro recorder for 1 day and then
post that code and we can help you generalize it.

Peter Richardson

"DB74" wrote:

I need to code a macro that will open a file dated yesterday copy data and
paste it into todays file so I can compare changes from yesterday to today.
I save all the files with a date stamp, so I want to open only the most
recent file, but if it s a Monday, I need it to open Friday's data...any
suggestions?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default macro code to open a file dated yesterday

The filesearch method should work.

Set fs = Application.FileSearch
With fs
.LookIn = "C:\My Documents"
.SearchSubFolders = True
.LastModified = msoLastModifiedYesterday
End With


"barnabel" wrote:

The first thing that we would need to know is how do you express the date in
the file name? Is it yymmdd, yyyymmdd, mmddyyyy, ddmmyyyy? Personally I
would name them yyyymmdd so that they sort correctly. So assume the file is
called 20070905log.xls

Next where is the data that needs to be copied?

finally where does it need to go so you can compare it? A separate sheet or
a column on an existing sheet? Are the sheets named?

Answering these questions will get you on the way to a solutions. Try
recording what you want to do using the macro recorder for 1 day and then
post that code and we can help you generalize it.

Peter Richardson

"DB74" wrote:

I need to code a macro that will open a file dated yesterday copy data and
paste it into todays file so I can compare changes from yesterday to today.
I save all the files with a date stamp, so I want to open only the most
recent file, but if it s a Monday, I need it to open Friday's data...any
suggestions?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default macro code to open a file dated yesterday

Thank you for the quick reply...I am saving them as "filename 2007-9-8"...I
want to save them into today't file that has already been created by my macro
"filename 2007-9-9"...

The data is about 19000 row from colmuns A thru T and I want to paste it at
the bottom of todays data to run a pivot and compare yesterdays data to
today's...also, it is not a consistant # of rows, one day it might be 18000
raow, and the next it might be 20,000...

I will record the actions tomorrow when I am at the office and see what the
code looks like...

Thanks again for your help!

"barnabel" wrote:

The first thing that we would need to know is how do you express the date in
the file name? Is it yymmdd, yyyymmdd, mmddyyyy, ddmmyyyy? Personally I
would name them yyyymmdd so that they sort correctly. So assume the file is
called 20070905log.xls

Next where is the data that needs to be copied?

finally where does it need to go so you can compare it? A separate sheet or
a column on an existing sheet? Are the sheets named?

Answering these questions will get you on the way to a solutions. Try
recording what you want to do using the macro recorder for 1 day and then
post that code and we can help you generalize it.

Peter Richardson

"DB74" wrote:

I need to code a macro that will open a file dated yesterday copy data and
paste it into todays file so I can compare changes from yesterday to today.
I save all the files with a date stamp, so I want to open only the most
recent file, but if it s a Monday, I need it to open Friday's data...any
suggestions?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default macro code to open a file dated yesterday

And if Friday is a holiday?

Or if the workbook isn't created for a week while you're on a business trip?

I'd just look through the last few days and look for the one that matches the
closest date.

Option Explicit
Sub testme()

Dim wkbk As Workbook
Dim myDate As Date
Dim myPath As String
Dim myPfx As String

myPfx = "Filename " 'includes trailing space

myPath = "C:\my documents\excel\"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

Set wkbk = Nothing

'check about 100 dates
For myDate = Date - 1 To Date - 100 Step -1
On Error Resume Next
Set wkbk = Workbooks.Open(myPath & myPfx & _
Format(myDate, "yyyy-m-d") & ".xls")
On Error GoTo 0
If wkbk Is Nothing Then
'keep looking
Else
'found it
Exit For
End If
Next myDate

If wkbk Is Nothing Then
MsgBox "never found!"
Exit Sub
End If

'do the work against wkbk
wkbk.Worksheets("Someworksheetnamehere").Range("so merange").Copy _
ThisWorkbook.Worksheets("somesheet").Range("someot herrng")

wkbk.Close SaveChanges:=False

End Sub

DB74 wrote:

Thank you for the quick reply...I am saving them as "filename 2007-9-8"...I
want to save them into today't file that has already been created by my macro
"filename 2007-9-9"...

The data is about 19000 row from colmuns A thru T and I want to paste it at
the bottom of todays data to run a pivot and compare yesterdays data to
today's...also, it is not a consistant # of rows, one day it might be 18000
raow, and the next it might be 20,000...

I will record the actions tomorrow when I am at the office and see what the
code looks like...

Thanks again for your help!

"barnabel" wrote:

The first thing that we would need to know is how do you express the date in
the file name? Is it yymmdd, yyyymmdd, mmddyyyy, ddmmyyyy? Personally I
would name them yyyymmdd so that they sort correctly. So assume the file is
called 20070905log.xls

Next where is the data that needs to be copied?

finally where does it need to go so you can compare it? A separate sheet or
a column on an existing sheet? Are the sheets named?

Answering these questions will get you on the way to a solutions. Try
recording what you want to do using the macro recorder for 1 day and then
post that code and we can help you generalize it.

Peter Richardson

"DB74" wrote:

I need to code a macro that will open a file dated yesterday copy data and
paste it into todays file so I can compare changes from yesterday to today.
I save all the files with a date stamp, so I want to open only the most
recent file, but if it s a Monday, I need it to open Friday's data...any
suggestions?


--

Dave Peterson


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
File saved yesterday is blank today, how do I retrieve? MelB New Users to Excel 2 February 12th 10 03:56 PM
Excel file - dissappeared since yesterday Bob Vancouver Excel Discussion (Misc queries) 3 January 23rd 09 09:56 PM
open a file automatically which was created yesterday davethewelder Excel Discussion (Misc queries) 8 March 7th 08 11:52 AM
Saving a file to have the filename of the dated it was created Dave[_65_] Excel Programming 1 May 17th 06 10:42 AM
Insert VBA code with a macro in a .xls file by workbook open event mihai[_3_] Excel Programming 8 July 29th 04 01:49 PM


All times are GMT +1. The time now is 12:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"