Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default last day of month in prior month

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default last day of month in prior month

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default last day of month in prior month

=date does not work in VBA
tempdate =DATE(YEAR(A1),MONTH(A1),1)-1
produces a compilation error

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default last day of month in prior month

Ah, I see. That was a function based method. This works for me:

Dim xdate As Date
xdate = Now()
MsgBox CDate("01/" & Month(xdate) & "/" & Year(xdate)) - 1

Swap month & date if you're American.

"thomas donino" wrote:

=date does not work in VBA
tempdate =DATE(YEAR(A1),MONTH(A1),1)-1
produces a compilation error

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default last day of month in prior month

Here is the current code which is not working

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
Dim targMo As Integer
Dim targYr As Integer
Dim tempdate As Date

targMo = Month(TargDate) - 1
targYr = Year(TargDate)
tempdate = DateValue(TargDate)
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))
LastDayInLastMo = tempdate
End Function

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default last day of month in prior month

Change thsi:
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))

to this
tempdate = CDate(CStr(targMo) & "/01/" & CStr(targYr))-1


"thomas donino" wrote:

Here is the current code which is not working

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
Dim targMo As Integer
Dim targYr As Integer
Dim tempdate As Date

targMo = Month(TargDate) - 1
targYr = Year(TargDate)
tempdate = DateValue(TargDate)
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))
LastDayInLastMo = tempdate
End Function

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default last day of month in prior month

thank you

"Sam Wilson" wrote:

Change thsi:
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))

to this
tempdate = CDate(CStr(targMo) & "/01/" & CStr(targYr))-1


"thomas donino" wrote:

Here is the current code which is not working

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
Dim targMo As Integer
Dim targYr As Integer
Dim tempdate As Date

targMo = Month(TargDate) - 1
targYr = Year(TargDate)
tempdate = DateValue(TargDate)
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))
LastDayInLastMo = tempdate
End Function

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default last day of month in prior month

Here is a simpler function for you to consider...

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
LastDayInLastMo = DateSerial(Year(TargDate), Month(TargDate), 0)
End Function

--
Rick (MVP - Excel)


"thomas donino" wrote in message
...
thank you

"Sam Wilson" wrote:

Change thsi:
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))

to this
tempdate = CDate(CStr(targMo) & "/01/" & CStr(targYr))-1


"thomas donino" wrote:

Here is the current code which is not working

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
Dim targMo As Integer
Dim targYr As Integer
Dim tempdate As Date

targMo = Month(TargDate) - 1
targYr = Year(TargDate)
tempdate = DateValue(TargDate)
tempdate = CDate(CStr(targMo) & ", " & CStr(0) & ", " & CStr(targYr))
LastDayInLastMo = tempdate
End Function

"Sam Wilson" wrote:

Parse into year, month & the number 1 then take a day off - otherwise
a 30
day month after a 31 day month will cause you problems etc...

=DATE(YEAR(A1),MONTH(A1),1)-1


"thomas donino" wrote:

I am having trouble writing a function that returns the last day of
the month
prior to the month from the date in the cell.

I am parsing the date apart into 3 variables, year,date,month
subtracting 1 from month
where i am having trouble is how to put it back together

thank you


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default last day of month in prior month

On Wed, 12 Aug 2009 11:09:39 -0400, "Rick Rothstein"
wrote:

Here is a simpler function for you to consider...

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
LastDayInLastMo = DateSerial(Year(TargDate), Month(TargDate), 0)
End Function

--
Rick (MVP - Excel)



Or, perhaps even simpler, using only one function and one arithmetic operation.

==================
Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
LastDayInLastMo = TargDate - Day(TargDate)
End Function
===================
--ron
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default last day of month in prior month

Here is a simpler function for you to consider...

Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
LastDayInLastMo = DateSerial(Year(TargDate), Month(TargDate), 0)
End Function


Or, perhaps even simpler, using only one function and one arithmetic
operation.

==================
Public Function LastDayInLastMo(TargDate As Date) As Date
'get the last day of the month prior to the date in the cell
LastDayInLastMo = TargDate - Day(TargDate)
End Function
===================


Yep, that is definitely a better formula to use.

--
Rick (MVP - Excel)

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
Delete Rows Prior to Last Month Alison Excel Programming 1 February 4th 09 12:10 PM
15th of prior month Deb Excel Worksheet Functions 3 February 6th 07 09:48 PM
Date arithmetic: adding 1 month to prior end of month date manxman Excel Worksheet Functions 2 July 14th 06 09:29 PM
dates, 1 month prior wfactor Excel Worksheet Functions 5 June 19th 06 08:23 AM
Prior Month Howard Excel Discussion (Misc queries) 6 November 2nd 05 03:06 PM


All times are GMT +1. The time now is 05: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"