+ Reply to Thread
Results 1 to 6 of 6

Extract the MONTH of a cell value with Excel VBA

  1. #1
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    268

    Extract the MONTH of a cell value with Excel VBA

    Hello everyone!

    On B5 cell, I have the following value: 2015-04-01 , so it is in DATE format.

    I want to get the value 04 exactly as 04 , but it is returning 4! I should get 04, with zero!

    Here is what I tried:
    Please Login or Register  to view this content.
    Even using the following, it is not working:
    Please Login or Register  to view this content.

    This must be Integer or Long.

    Can anyone help me, please?

    Thanks!
    Last edited by trizzo; 05-04-2015 at 10:06 AM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Extract the MONTH of a cell value with Excel VBA

    Does this help?

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    268

    Re: Extract the MONTH of a cell value with Excel VBA

    Thanks for quick response John! Ok, but, it is a Variant variable which shows "04" what if I need to get that as Integer or Long? 04 instead of "04".

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Extract the MONTH of a cell value with Excel VBA

    Dim as long instead of variant, though it will drop the leading 0. You can format how it is displayed, but not the actual value (length) of the variable.

    Assuming you are pasting it somewhere, or displaying in a message, then you could msgbox format(MonthHistory,"00")
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Extract the MONTH of a cell value with Excel VBA

    What do you intend too do with the variable?

  6. #6
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    268

    Re: Extract the MONTH of a cell value with Excel VBA

    Sorry Guys, the solution provided by John worked great, I am actually using a number without 0 in front of it now.
    Thanks everyone for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to extract the month name from a cell using VB code?
    By GIS2013 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-28-2013, 09:34 AM
  2. [SOLVED] Extract Year or Month only from a cell with date and time
    By djaurit in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-28-2013, 04:03 PM
  3. [SOLVED] How can I extract name of the LONG MONTH from previous cell.
    By AMIT in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-18-2006, 08:45 AM
  4. How do I extract just the month
    By kurt in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. [SOLVED] How to extract month number from month name
    By PM in forum Excel General
    Replies: 2
    Last Post: 01-19-2005, 12:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1