Get Month name from Date value in cell


"Excel VBA Help"

 


Play Friv Now - Play Friv Games
Go Back   The Code Cage Forums > Microsoft Topics > Microsoft Excel Forum
Our Info Pages:-
The Code Cage - Microsoft Office help

Notices

Microsoft Excel Forum Members Post questions in this forum if they are related to using Microsoft Excel, this includes Worksheet Functions, Excel VBA Programming, Simple Macro Code & Charting, Excel formula etc.

New Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Unread 18th April 2010, 23:06
Chip Dukes's Avatar
Chip Dukes
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default Get Month name from Date value in cell
     Excel: VBA   


------ Register to get rid of these "In Post" ads! ------

Category: Excel: VBA   

I'm trying to get the month from a date representation as follows ...
VBA CODE:
Private Sub Worksheet_Change(ByVal Target As Range) 
     
    If Target.Column = 3 And Target.Cells.Count = 1 Then 
        Dim strDate As String 
        strDate = Format(Target.Value, "mmmm") 
        Range("A20").Value = strDate 
    Else 
        Exit Sub 
    End If 
     
End Sub 
The error I am getting is ... "Object Required"

on the ... strDate = Format(Target.Value, "mmmm") ...

line

by which I'm guessing Target.Value is not being seen as a date type ...

Any ideas as to how I could get the month from a value entered into a call
.... the value will always be in the form 5/24/55 ... from which I need the
string "May"

Chip Dukes
(New to Excel programming)


Last edited by RoyUK; 11th May 2010 at 18:23.
The Code Cage Advertisment
Advertisement
  #2  
Unread 18th April 2010, 23:30
JLatham's Avatar
JLatham
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default RE: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


Your code works fine for me with column C formatted as a date. Might try
changing to this code to see if it helps identify the problem for you.

Your code is in the worksheet's code module, isn't it? You can check by
right-clicking the sheet's name tab and choosing [View Code] from the list.
Your code should be visible when the VB Editor opens.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 And Target.Cells.Count = 1 Then
Dim strDate As String
If IsDate(Target) Then
strDate = Format(Target.Value, "mmmm")
Range("A20").Value = strDate
Else
MsgBox "Not a date: " & Target.Value
End If
Else
Exit Sub
End If

End Sub


"Chip Dukes" wrote:

> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


  #3  
Unread 18th April 2010, 23:30
Barb Reinhardt's Avatar
Barb Reinhardt
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default RE: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


Every time you make a change to the worksheet, the worksheet change event is
called. The only problem I see is updated below.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 And Target.Cells.Count = 1 Then
Dim strDate As String
strDate = Format(Target.Value, "mmmm")
Application.EnableEvents= False 'Added
Range("A20").Value = strDate
Application.EnableEvents= True 'Added
Else
Exit Sub
End If

--
HTH,

Barb Reinhardt



"Chip Dukes" wrote:

> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


The Code Cage Advertisment
Advertisement

To stop seeing these ads and get other benefits check This page!

  #4  
Unread 18th April 2010, 23:30
Barb Reinhardt's Avatar
Barb Reinhardt
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default RE: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


You may want to test target for a value as well. Or do something like this

if not isempty(target) then

'Do something
end if

--
HTH,

Barb Reinhardt



"Chip Dukes" wrote:

> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


  #5  
Unread 18th April 2010, 23:30
FSt1's Avatar
FSt1
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default RE: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


hi
i copied your code. when i enter a date in column c (target column), it puts
april in A20 so the code seems to be working fine.
what are you doing to trigger the code??

regards
FSt1

"Chip Dukes" wrote:

> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


  #6  
Unread 18th April 2010, 23:40
Joe User's Avatar
Joe User
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default RE: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


"Chip Dukes" wrote:
> Any ideas as to how I could get the month from a
> value entered into a call ... the value will always be
> in the form 5/24/55 ... from which I need the string "May"


The basic paradigm -- strDate = Format(Target.Value, "mmmm") -- works fine
for me, whether Target.Value is a date serial number or even a string
recognized as a date.

But it seems odd to me that you are assigning the result to the same cell --
Range("A20") -- for any single-cell change in column 3.

Not sure if you dumbed down the code fragment in an effort to isolate the
error, or if you are inadvertently executing the Format conversion for
unexpected cells in column 3.

(Even so, I have yet to encounter a situation where that code would result
in an "object required" runtime error. Nonetheless....)

If you are looking for a change in a specific cell, you might change your If
expression to something of the form Target = Range("C1").

If that does not solve the problem, feel free to send your workbook to me.
Send it to joe2004 "at" hotmail.com.

Aside.... It would be prudent to disable events when changing a cell within
an event macro. To wit:

Dim strDate As String
Application.EnableEvents = False
strDate = Format(Target.Value, "mmmm")
Range("A20").Value = strDate
Application.EnableEvents = True

Also, `Else: Exit Sub` is superfluous in your code fragement. Again, not
sure if that's the result of simplifying the example.


----- original message -----

"Chip Dukes" wrote:
> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


The Code Cage Advertisment
Advertisement

To stop seeing these ads and get other benefits check This page!

  #7  
Unread 18th April 2010, 23:50
Steve Yandl's Avatar
Steve Yandl
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default Re: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


What I've got below tested fine and tests to make sure the value that was
entered can be seen as a date.

'-------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 And Target.Cells.Count = 1 And _
IsDate(Target.Value) Then

Dim dteDate As Date
Dim strDate As String

strDate = CStr(Target.Value)
dteDate = CDate(strDate)
Range("A20").Value = Format(dteDate, "mmmm")

Else
Exit Sub
End If

End Sub

'-------------------------------------

Steve Yandl



"Chip Dukes" <ChipDukes@discussions.microsoft.com> wrote in message
news:4347D552-F81B-42F3-B181-4741750A5A64@microsoft.com...
> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


  #8  
Unread 19th April 2010, 01:25
Rick Rothstein's Avatar
Rick Rothstein
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default Re: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


Others have addressed your main question, I just thought I would mention to
you (and the others out there who weren't aware of it) that VB has a
MonthName function... it takes the month number as its first argument and an
optional Boolean second argument letting you control whether the full or
abbreviated month name it returned. So, the code in the True portion of your
If/Then/Else block could have be this...

Range("A20").Value = MonthName(Month(Target.Value))

--
Rick (MVP - Excel)



"Chip Dukes" <ChipDukes@discussions.microsoft.com> wrote in message
news:4347D552-F81B-42F3-B181-4741750A5A64@microsoft.com...
> I'm trying to get the month from a date representation as follows ...
>
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> If Target.Column = 3 And Target.Cells.Count = 1 Then
> Dim strDate As String
> strDate = Format(Target.Value, "mmmm")
> Range("A20").Value = strDate
> Else
> Exit Sub
> End If
>
> End Sub
>
> The error I am getting is ... "Object Required"
>
> on the ... strDate = Format(Target.Value, "mmmm") ...
>
> line
>
> by which I'm guessing Target.Value is not being seen as a date type ...
>
> Any ideas as to how I could get the month from a value entered into a call
> ... the value will always be in the form 5/24/55 ... from which I need the
> string "May"
>
> Chip Dukes
> (New to Excel programming)
>


  #9  
Unread 19th April 2010, 01:51
FSt1's Avatar
FSt1
Newsgroup Contributor
 
Posts: n/a
Posts Per Day: 0
My Top Tip Count:
Default Re: Get Month name from Date value in cell

------ Register to get rid of these "In Post" ads! ------


and just to add for those who might be interested.
it also has a weekday function.

Range("A20").value = WeekdayName(Weekday(Target.Value)

FSt1

"Rick Rothstein" wrote:

> Others have addressed your main question, I just thought I would mention to
> you (and the others out there who weren't aware of it) that VB has a
> MonthName function... it takes the month number as its first argument and an
> optional Boolean second argument letting you control whether the full or
> abbreviated month name it returned. So, the code in the True portion of your
> If/Then/Else block could have be this...
>
> Range("A20").Value = MonthName(Month(Target.Value))
>
> --
> Rick (MVP - Excel)
>
>
>
> "Chip Dukes" <ChipDukes@discussions.microsoft.com> wrote in message
> news:4347D552-F81B-42F3-B181-4741750A5A64@microsoft.com...
> > I'm trying to get the month from a date representation as follows ...
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> >
> > If Target.Column = 3 And Target.Cells.Count = 1 Then
> > Dim strDate As String
> > strDate = Format(Target.Value, "mmmm")
> > Range("A20").Value = strDate
> > Else
> > Exit Sub
> > End If
> >
> > End Sub
> >
> > The error I am getting is ... "Object Required"
> >
> > on the ... strDate = Format(Target.Value, "mmmm") ...
> >
> > line
> >
> > by which I'm guessing Target.Value is not being seen as a date type ...
> >
> > Any ideas as to how I could get the month from a value entered into a call
> > ... the value will always be in the form 5/24/55 ... from which I need the
> > string "May"
> >
> > Chip Dukes
> > (New to Excel programming)
> >

> .
>


The Code Cage Advertisment
Advertisement

To stop seeing these ads and get other benefits check This page!

New Reply
The Code Cage at Facebookhe Code Cage at Twitter


Tags
cell, date, month

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

The Code Cage Affilliates



http://www.thecodecage.com/forumz tested by McAfee Security http://www.thecodecage.com/forumz tested by McAfee Security

All times are GMT +1. The time now is 18:56.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
No part of this board, functionality or creatives may be copied or reproduced either in part or full without the express permission of The Code Cage Team.
Copyright ©2008 - , Simon lloyd.
We are not associated with nor employed by Microsoft in any way, we simply provide resources!
All MS office icons are registered trademarks of the application they represent and are used with permission from Microsoft
UA-8135493-1
Cultural Forum | Study at Malaysian University