Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Hi, i save a report to a folder on a weekly basis, called
"gAMS_Report_Oct_06.xls". With a new month i create a new file for the
new month but also need to load the last months file to do a comparison,
any idea's to do this with code ??

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Select last months file

Try this UDF to get the previous motnth file name:

Sub test()
fname = prevmonthfname("gAMS_Report_Okt_06.xls")
End Sub
Function prevmonthfname(thismonthfname)
ho = Mid(thismonthfname, 13, 3) & "."
ev = Mid(thismonthfname, 17, 2) & "."
kelt = DateValue(ev & ho & 1)
rkelt = DateAdd("m", -1, kelt)
elho = Format(rkelt, "mmm")
elev = Format(rkelt, "yy")
prevmonthfname = "gAMS_Report_" & elho & "_" & elev & ".xls"
End Function

Regards,
Stefi


€˛Les Stout€¯ ezt Ć*rta:

Hi, i save a report to a folder on a weekly basis, called
"gAMS_Report_Oct_06.xls". With a new month i create a new file for the
new month but also need to load the last months file to do a comparison,
any idea's to do this with code ??

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Select last months file

Const sFile As String = "gAMS_Report_<Month_06.xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, "<Month", Format(Date, "mmm"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, "<Month", Format(Date - Day(Date), "mmm"))
Set oWBLast = Workbooks.Open(sFile)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Hi, i save a report to a folder on a weekly basis, called
"gAMS_Report_Oct_06.xls". With a new month i create a new file for the
new month but also need to load the last months file to do a comparison,
any idea's to do this with code ??

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Hi Stefi,

sorry i get an error of "Type mismach" when i run this at "kelt"

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Select last months file

Hi Bob,

It's a nice and compact solution but I'm afraid it will not work in January:
it returns December of the same year instead of December of the previous year!

Regards,
Stefi

€˛Bob Phillips€¯ ezt Ć*rta:

Const sFile As String = "gAMS_Report_<Month_06.xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, "<Month", Format(Date, "mmm"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, "<Month", Format(Date - Day(Date), "mmm"))
Set oWBLast = Workbooks.Open(sFile)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Hi, i save a report to a folder on a weekly basis, called
"gAMS_Report_Oct_06.xls". With a new month i create a new file for the
new month but also need to load the last months file to do a comparison,
any idea's to do this with code ??

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Thanks a million Bob, works a treat..

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Select last months file

Sorry, that's because I have a different date setting, here is a version for
US date setting:
Sub test()
fname = prevmonthfname("gAMS_Report_Oct_06.xls")
End Sub
Function prevmonthfname(thismonthfname)
fnamemonth = Mid(thismonthfname, 13, 3) & "/"
fnameyear = Mid(thismonthfname, 17, 2)
fnamedate = DateValue(fnamemonth & "1/" & fnameyear)
prevdate = DateAdd("m", -1, fnamedate)
prevmonth = Format(prevdate, "mmm")
prevyear = Format(prevdate, "yy")
prevmonthfname = "gAMS_Report_" & prevmonth & "_" & prevyear & ".xls"
End Function

Regards,
Stefi

€˛Les Stout€¯ ezt Ć*rta:

Hi Stefi,

sorry i get an error of "Type mismach" when i run this at "kelt"

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Select last months file

Les,

updated version to cater for year-end

Const sFile As String = "gAMS_Report_<Month.xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, "<Month", Format(Date, "mmm-yy"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, "<Month", Format(Date - Day(Date),
"mmm-yy"))
Set oWBLast = Workbooks.Open(sFile)

Stefi, thanks for that, I missed the significance of the 06 in the filename
(doh!).

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Thanks a million Bob, works a treat..

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Select last months file

Bob, I think one more slight modification is needed: Les uses "_" as
separator so
"mmm_yy" format code should be used in Format functions!

Regards,
Stefi

€˛Bob Phillips€¯ ezt Ć*rta:

Les,

updated version to cater for year-end

Const sFile As String = "gAMS_Report_<Month.xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, "<Month", Format(Date, "mmm-yy"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, "<Month", Format(Date - Day(Date),
"mmm-yy"))
Set oWBLast = Workbooks.Open(sFile)

Stefi, thanks for that, I missed the significance of the 06 in the filename
(doh!).

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Thanks a million Bob, works a treat..

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Thanks very much Bob and Stefi, i have tried to replace the <file with
a variable and it does not like it, could you help please ?

Const sFile As String = "gAMS_Report_" & Month &".xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, Month, Format(Date, "mmm-yy"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, Month, Format(Date - Day(Date),
"mmm-yy"))
Set oWBLast = Workbooks.Open(sFile)


Les Stout

*** Sent via Developersdex http://www.developersdex.com ***


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Select last months file

Les,

Notwithstanding that Month is not a good name for a variable, what is in
Month?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Thanks very much Bob and Stefi, i have tried to replace the <file with
a variable and it does not like it, could you help please ?

Const sFile As String = "gAMS_Report_" & Month &".xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, Month, Format(Date, "mmm-yy"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, Month, Format(Date - Day(Date),
"mmm-yy"))
Set oWBLast = Workbooks.Open(sFile)


Les Stout

*** Sent via Developersdex http://www.developersdex.com ***



  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Select last months file

use Bob's code as written

Const sFile As String = "gAMS_Report_<Month.xls"


You are making Month a variable - but the Literal "<Month"

should be retained in the string. The substitution for the actual month is
made in this line:

sFilename = Replace(sFile, "<Month", Format(Date, "mmm-yy"))

--
Regards,
Tom Ogilvy


"Les Stout" wrote:

Thanks very much Bob and Stefi, i have tried to replace the <file with
a variable and it does not like it, could you help please ?

Const sFile As String = "gAMS_Report_" & Month &".xls"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook

sFilename = Replace(sFile, Month, Format(Date, "mmm-yy"))
Set oWBThis = Workbooks.Open(sFile)
sFilename = Replace(sFile, Month, Format(Date - Day(Date),
"mmm-yy"))
Set oWBLast = Workbooks.Open(sFile)


Les Stout

*** Sent via Developersdex http://www.developersdex.com ***

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Hi Tom, if i do that i get an error msg "Constant Expression Required"
??

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Select last months file

Hi Bob, I am sure your code will work, sorry as said before i am not a
programmer and did not understand the <Date. I however do get an error
as per my last thread.

best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Select last months file

As I said, what is in that variable Month?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Hi Bob, I am sure your code will work, sorry as said before i am not a
programmer and did not understand the <Date. I however do get an error
as per my last thread.

best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***





  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Select last months file

Hi Bob,

I have the month in format mmm. The variable is actually "ThisMonth"

regards,

Les

Bob Phillips wrote:
As I said, what is in that variable Month?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Hi Bob, I am sure your code will work, sorry as said before i am not a
programmer and did not understand the <Date. I however do get an error
as per my last thread.

best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***


  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Select last months file

This might take you forward

Const sFile As String = "gAMS_Report_"
Dim sFilename As String
Dim sThis As Workbook
Dim sLast As Workbook
Dim ThisMonth As String

ThisMonth = "Oct"
sFilename = sFile & Format(DateValue("01-" & ThisMonth & "-" &
Year(Date)), "mmm_yy")
Set oWBThis = Workbooks.Open(sFile)
sFilename = sFile & Format(DateValue("01-" & ThisMonth & "-" &
Year(Date)) - 1, "mmm_yy")
Set oWBLast = Workbooks.Open(sFile)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les" wrote in message
ups.com...
Hi Bob,

I have the month in format mmm. The variable is actually "ThisMonth"

regards,

Les

Bob Phillips wrote:
As I said, what is in that variable Month?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Les Stout" wrote in message
...
Hi Bob, I am sure your code will work, sorry as said before i am not a
programmer and did not understand the <Date. I however do get an

error
as per my last thread.

best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***




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
How to print select months from worksheet pinkluxe Excel Discussion (Misc queries) 2 September 30th 08 01:30 AM
How do I select 13 months from a data range for charts marg Charts and Charting in Excel 3 March 14th 07 06:30 AM
Prompt user to select file with default file selected dialog Bruce Cooley Excel Programming 0 September 15th 03 06:43 AM
Prompt user to select file with default file selected dialog Bob Phillips[_5_] Excel Programming 0 September 14th 03 09:22 PM
Prompt user to select file with default file selected dialog Bob Phillips[_5_] Excel Programming 0 September 14th 03 09:19 PM


All times are GMT +1. The time now is 04:54 AM.

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"