+ Reply to Thread
Results 1 to 9 of 9

Problems assigning date data to cells

  1. #1
    Registered User
    Join Date
    06-22-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Problems assigning date data to cells

    I have this problem which has me stumped.

    I want to show a date in the excel cell in the format "Jan-13" (for 1st January 2013). Instead I get an integer. The integer does not correspond to the date (41275 in this case), instead the integer represents, in this example, 13th January 2012 (40921). I cannot for the life of me understand where this number comes from.

    Here's the code (not optimised, rather set up to debug this issue!). :

    Dim Month As Range
    Dim r As Integer
    Dim Temp As Variant
    Set Month = Application.InputBox(Prompt:= _
    "Please select a range of months from a COLUMN", _
    Title:="SPECIFY MONTHS", Type:=8)
    r = 1
    With Worksheets("MyWorksheetName")
    ' Displays, e.g. "41275" - good
    MsgBox ("Month: " & Month.Value2(x, 1))
    ' Displays, e.g. "Jan-13" - good
    MsgBox ("Formatted Month: " & Format(Month.Value2(x, 1), "MMM-yy"))
    Temp = Format(Month.Value2(x, 1), "MMM-yy")
    ' Displays, e.g. "Jan-13" - good
    MsgBox ("Temp: " & Temp)
    ' "howdy" appears as entry in cell A1 in Excel spreadsheet - good
    .Cells(r, 1) = "howdy"
    ' e.g. "40921" appears as entry in cell A1 in Excel spreadsheet - BAD!
    .Cells(r, 1) = Temp
    ' Displays, e.g. "40921" - BAD!
    MsgBox ("Cells Month value: " & .Cells(r, 1))


    Another interesting point. If I assign the value to .Cells(r, 6), i.e. to a different column, then I get the same value, but it shows in Excel in a different format, "dd-MMM".

    Any help understanding this would be much appreciated.

    Mailbags
    Last edited by Mailbags; 07-02-2012 at 10:38 PM.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Problems assigning date data to cells

    did you read the forum rules re code tags?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    06-22-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problems assigning date data to cells

    OK, thanks for the tip. Here is is again:
    ----

    I have this problem which has me stumped.

    I want to show a date in the excel cell in the format "Jan-13" (for 1st January 2013). Instead I get an integer. The integer does not correspond to the date (41275 in this case), instead the integer represents, in this example, 13th January 2012 (40921). I cannot for the life of me understand where this number comes from.

    Here's the code (not optimised, rather set up to debug this issue!). :

    Please Login or Register  to view this content.
    Another interesting point. If I assign the value to .Cells(r, 6), i.e. to a different column, then I get the same value, but it shows in Excel in a different format, "dd-MMM".

    Any help understanding this would be much appreciated.

    Mailbags

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Problems assigning date data to cells

    You should have posted enough code to compile and run. Actually, a sample workbook is needed rather than guessing what's in it.
    Last edited by protonLeah; 06-22-2012 at 11:23 PM.
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    06-22-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problems assigning date data to cells

    OK, bear with me. I am rushing this too much. Sorry, I realise its not very helpful.

    Try this workbook:
    - Run the "Combinations" macro.
    - when prompted, select at least two months from the "Constants" page (suggest Jan-13 and Feb-13)
    - Output appears on the MyWorksheet page

    Test_Combinations.xlsm

    Full macro code:

    Please Login or Register  to view this content.
    thanks.
    Last edited by Mailbags; 06-23-2012 at 01:50 AM.

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems assigning date data to cells

    Try this.
    Please Login or Register  to view this content.
    Last edited by JapanDave; 06-23-2012 at 03:47 AM.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  7. #7
    Registered User
    Join Date
    06-22-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problems assigning date data to cells

    That helps a little, in that the correct date is now entered into the cell. However the format is not what I require (d-MMM).

    So I tried changing the final write to the cell, your:
    .Cells(r, 1).Value = Temp

    to
    .Cells(r, 1).Value = Format(Temp, "MMM-yy")

    ... with the result that the format is unchanged but now the date is incorrect again. So in the example of 1st Jan 2013, with your code, the cell shows "1-Jan", with my change is says "13-Jan".

    (by the way, using .Value here makes no difference in either case; leave it out and its the same result)


    There is something going on with the Format command and its return value which I dont understand.

  8. #8
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems assigning date data to cells

    How much are you going to pay me for this!!!!
    Here in Japan , date is a big pain in the butt, as it is different to every where else in the world, so I feel your pain. Anyway see if this helps you any.

    Please Login or Register  to view this content.
    Last edited by JapanDave; 06-23-2012 at 11:15 AM.

  9. #9
    Registered User
    Join Date
    06-22-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Problems assigning date data to cells

    Thanks, JapanDave, your time in Japan has not been wasted. It works a treat.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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