Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Starting Date of the Month

Hi all,

How can I change the date of a given cell to be the first
day of that month?

for eg. if I enter in A1 16-2-2002 I want to get 01-2-
2002 in the same cell

if I use =DATE(YEAR($A$1),MONTH($A$1),1) in my B1 i will
get the first day of the date entered in A1. but i want
to get it in A1 itself using code?


Any help?

TIA
Soniya
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 90
Default Starting Date of the Month

How about just hiding column A or setting its width to zero. Personally
in this situation I move column A far off to the right, leave column B
(which now is column A) prominent, and set the print area appropriately.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Starting Date of the Month

Soniya wrote:
Hi all,

How can I change the date of a given cell to be the first day of that
month?

for eg. if I enter in A1 16-2-2002 I want to get 01-2- 2002 in the
same cell

if I use =DATE(YEAR($A$1),MONTH($A$1),1) in my B1 i will get the
first day of the date entered in A1. but i want to get it in A1
itself using code?


Any help?

TIA Soniya

Sounds like you need to use the onchange event:

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 1) Then
If (IsDate(Target.Text)) Then
Target = (CStr(Month(Target.Text)) + "-01-" +
CStr(Year(Target.Text)))
End If
End If
End Sub

Note: My email client insists on wrapping the assignment line.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Starting Date of the Month

On Wed, 20 Aug 2003 02:51:09 -0700, "Soniya" wrote:

Hi all,

How can I change the date of a given cell to be the first
day of that month?

for eg. if I enter in A1 16-2-2002 I want to get 01-2-
2002 in the same cell

if I use =DATE(YEAR($A$1),MONTH($A$1),1) in my B1 i will
get the first day of the date entered in A1. but i want
to get it in A1 itself using code?


Any help?

TIA
Soniya


You need to use an event triggered macro.

Example to ensure that dates entered in the range A1:A10 will be converted to
the first of the month:

Right click on the sheet tab and select View Code.
Paste the code below into the window that opens.
Note that the error checking is very primitive, as I was not sure of what you
wanted to do here, and/or whether a data validation routine would be
appropriate.

====================
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim AOI As Range, c As Range

Set AOI = [A1:A10]

If Intersect(Target, AOI) Is Nothing Then GoTo Done

For Each c In AOI
If IsDate(c.Value) Then
On Error GoTo ForNext
c.Value = DateSerial(Year(c.Value), Month(c.Value), 1)
End If
ForNext: Next c

Done: Application.EnableEvents = True
End Sub
==================


--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Starting Date of the Month

Jerry,
Try this

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 1) Then
If (IsDate(Target.Text)) Then
Target = (CStr(Month(Target.Text)) + "-01-" + _
CStr(Year(Target.Text)))
End If
msgbox "Date was changed"
End If
End Sub

It might give you some indication why Ron has put in

Application.EnableEvents = False
at the top of his code and reenabled them at the bottom.

--
Regards,
Tom Ogilvy


"Jerry Park" wrote in message
...
Soniya wrote:
Hi all,

How can I change the date of a given cell to be the first day of that
month?

for eg. if I enter in A1 16-2-2002 I want to get 01-2- 2002 in the
same cell

if I use =DATE(YEAR($A$1),MONTH($A$1),1) in my B1 i will get the
first day of the date entered in A1. but i want to get it in A1
itself using code?


Any help?

TIA Soniya

Sounds like you need to use the onchange event:

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 1) Then
If (IsDate(Target.Text)) Then
Target = (CStr(Month(Target.Text)) + "-01-" +
CStr(Year(Target.Text)))
End If
End If
End Sub

Note: My email client insists on wrapping the assignment line.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Starting Date of the Month

Yes. I was just giving basic code to accomplish the desired result.

Application.EnableEvents = False is good.

Actually, the code should also check for the possibility that multiple
cells have been selected (copy/paste) and react accordingly.



Tom Ogilvy wrote:
Jerry,
Try this

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 1) Then
If (IsDate(Target.Text)) Then
Target = (CStr(Month(Target.Text)) + "-01-" + _
CStr(Year(Target.Text)))
End If
msgbox "Date was changed"
End If
End Sub

It might give you some indication why Ron has put in

Application.EnableEvents = False
at the top of his code and reenabled them at the bottom.



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 get month number for fiscal year starting in Oct. Tim Excel Worksheet Functions 3 October 13th 08 05:08 PM
Date arithmetic: adding 1 month to prior end of month date manxman Excel Worksheet Functions 2 July 14th 06 09:29 PM
Sort month/date/year data using month and date only SMW820 Excel Discussion (Misc queries) 6 June 22nd 06 05:14 PM
=VLOOKUP(1,Nationality!B5:B29,IF(MONTH(date)6,MONTH(date)-6, MON Ali Excel Worksheet Functions 14 January 18th 06 08:20 AM
Average starting with first month Jim Excel Discussion (Misc queries) 7 October 29th 05 12:48 PM


All times are GMT +1. The time now is 06:38 PM.

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

About Us

"It's about Microsoft Excel"