Results 1 to 9 of 9

Thread: VB control Excel date format

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    5

    VB control Excel date format

    Dear Expert

    I want to output the recordset to Excel and set the date format in Excel dd-mm-yyyy. However, when I use below syntax, the outout of format is different under different case

    VB Code:
    1. Dim oExcel As Object
    2.     Dim oBook As Object
    3.     Dim oSheet As Object
    4.    
    5.  
    6.    
    7.     Set oExcel = CreateObject("Excel.Application")
    8.     Set oBook = oExcel.Workbooks.Add
    9.  
    10. With oSheet
    11.     For i = 0 To objRs.Fields.Count - 1
    12.         oSheet.Cells(1, i + 1).Value = Trim(CStr(objRs.Fields(i).Name))
    13.         objRs.MoveFirst
    14.        .Range("F1:F" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"
    15.        .Range("G1:G" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"
    16.     For j = 1 To objRs.RecordCount
    17.         If i = 5 Or i = 6 Then
    18.            oSheet.Cells(j + 1, i + 1).NumberFormat = "dd-mm-yyyy"
    19.         End If
    20.            oSheet.Cells(j + 1, i + 1).Value = objRs.Fields(i).Value
    21.            objRs.MoveNext
    22.     Next
    23.     Next
    24. End With
    25.  
    26.  
    27. ?objRs.Fields(i).Value
    28. 23-09-2005
    29. 02-09-2005
    30.  
    31.  
    32. ?oSheet.Cells(j + 1, i + 1).Value
    33. 23-09-2005
    34. 09-02-2005


    Even the date output in recordset is 02-09-2005, and the setting of the ouput of Excel is dd-mm-yyyy. The final output of Excel is still 09-02-2005.
    My PC regional setting is in dd-mm-yyyy.

    Any suggestion

    Thank you very much

    Brian choi
    Last edited by RobDog888; Oct 18th, 2005 at 10:37 PM. Reason: Added [vbcode] tags

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: VB control Excel date format

    try:

    format(.Range("F1:F" & objRs.RecordCount + 1), "dd-mm-yyyy")

    or

    format(cdate(objRs.Fields(i).Value), "dd-mm-yyyy")
    Last edited by D-niss; Oct 12th, 2005 at 11:04 PM.

  3. #3
    Lively Member
    Join Date
    May 2004
    Location
    malaysia
    Posts
    89

    Re: VB control Excel date format

    VB Code:
    1. ' you should not do numberformating when you insert result to each cells
    2. ' you should do number formating after finished insert records, this increase
    3. ' performance
    4.  
    5. ' use the following line to do number formatting
    6. ' xlWSheet = your worksheet
    7. ' Range("A:A"). = the range you want to format, here will format whole col A
    8. ' format
    9. ' you can define the range yourself
    10.  
    11. xlWSheet.Range("A:A").NumberFormat = "MM/dd/YYYY"
    hope this help.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    5

    Re: VB control Excel date format

    D-niss

    When I try
    format(.Range("F1:F" & objRs.RecordCount + 1), "dd-mm-yyyy")
    Type mismatch error

    When I try
    format(cdate(objRs.Fields(i).Value), "dd-mm-yyyy")
    The output is not in date

    Any idea?

  5. #5
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: VB control Excel date format

    I don't usually set the cell number format before passing the data, I do it at the same time, something like:

    With Sheet1
    Cells(j + 1, i + 1)= Format(objRs.Fields(i).Value, "mm-dd-yyy")
    End With

    It sets the correct format and correct value. In essence you wouldn't need this anymore:

    .Range("F1:F" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"
    .Range("G1:G" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"

    Also, I would check what is the data type of the date field you're trying to pass, it may shade more light on this.

    Best of luck

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    5

    Re: VB control Excel date format

    The problem was fixed by using below syntax

    With oSheet
    For i = 0 To objRs.Fields.Count - 1
    .Cells(1, i + 1).Value = objRs.Fields(i).Name
    objRs.MoveFirst
    For j = 1 To objRs.RecordCount
    If i = 5 Or i = 6 Then
    .Cells(j + 1, i + 1) = Format(objRs.Fields(i).Value, "dd-mmm-yyyy")
    'oSheet.Cells(j + 1, i + 1).Value = objRs.Fields(i).Value
    Else
    oSheet.Cells(j + 1, i + 1).Value = objRs.Fields(i).Value
    End If
    objRs.MoveNext
    Next
    Next
    .Range("F1:F" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"
    .Range("G1:G" & objRs.RecordCount + 1).NumberFormat = "dd-mm-yyyy"
    End With


    Thank you very much D-niss

  7. #7
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Re: VB control Excel date format

    Try Using MM instead of mm since mm is for minutes.
    so it should be like this: format(cdate(objRs.Fields(i).Value), "dd-MM-yyyy"


    Quote Originally Posted by brian_choi View Post
    D-niss

    When I try
    format(.Range("F1:F" & objRs.RecordCount + 1), "dd-mm-yyyy")
    Type mismatch error

    When I try
    format(cdate(objRs.Fields(i).Value), "dd-mm-yyyy")
    The output is not in date

    Any idea?

  8. #8
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: VB control Excel date format

    Hopefully the guy who started this thread in 2005 will see your update!

    (In your defense, it wasn't marked as "resolved.")

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB control Excel date format

    Try Using MM instead of mm since mm is for minutes.
    this is incorrect, the correct character for minutes is n, month is m, though in many cases m will work for minutes, if the representation is not ambiguous

    example
    Code:
    ?format(now,"m")
    3
    ?format(now,"n")
    47
    7:46 PM
    User-Defined Date/Time Formats (Format Function)


    The following table identifies characters you can use to create user-defined date/time formats:

    Character Description
    ( Time separator. In some locales, other characters may be used to represent the time separator. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character used as the time separator in formatted output is determined by your system settings.
    (/) Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.
    c Display the date as ddddd and display the time as
    ttttt, in that order. Display only date information if there is no fractional part to the date serial number; display only time information if there is no integer portion.
    d Display the day as a number without a leading zero (1 – 31).
    dd Display the day as a number with a leading zero (01 – 31).
    ddd Display the day as an abbreviation (Sun – Sat).
    dddd Display the day as a full name (Sunday – Saturday).
    ddddd Display the date as a complete date (including day, month, and year), formatted according to your system's short date format setting. The default short date format is m/d/yy.
    dddddd Display a date serial number as a complete date (including day, month, and year) formatted according to the long date setting recognized by your system. The default long date format is mmmm dd, yyyy.
    aaaa The same as dddd, only it's the localized version of the string.
    w Display the day of the week as a number (1 for Sunday through 7 for Saturday).
    ww Display the week of the year as a number (1 – 54).
    m Display the month as a number without a leading zero (1 – 12). If m immediately follows h or hh, the minute rather than the month is displayed.
    mm Display the month as a number with a leading zero (01 – 12). If m immediately follows h or hh, the minute rather than the month is displayed.
    mmm Display the month as an abbreviation (Jan – Dec).
    mmmm Display the month as a full month name (January – December).
    oooo The same as mmmm, only it's the localized version of the string.
    q Display the quarter of the year as a number (1 – 4).
    y Display the day of the year as a number (1 – 366).
    yy Display the year as a 2-digit number (00 – 99).
    yyyy Display the year as a 4-digit number (100 – 9999).
    h Display the hour as a number without leading zeros (0 – 23).
    Hh Display the hour as a number with leading zeros (00 – 23).
    N Display the minute as a number without leading zeros (0 – 59).
    Nn Display the minute as a number with leading zeros (00 – 59).
    S Display the second as a number without leading zeros (0 – 59).
    Ss Display the second as a number with leading zeros (00 – 59).
    t t t t t Display a time as a complete time (including hour, minute, and second), formatted using the time separator defined by the time format recognized by your system. A leading zero is displayed if the leading zero option is selected and the time is before 10:00 A.M. or P.M. The default time format is h:mm:ss.
    AM/PM Use the 12-hour clock and display an uppercase AM with any hour before noon; display an uppercase PM with any hour between noon and 11:59 P.M.
    am/pm Use the 12-hour clock and display a lowercase AM with any hour before noon; display a lowercase PM with any hour between noon and 11:59 P.M.
    A/P Use the 12-hour clock and display an uppercase A with any hour before noon; display an uppercase P with any hour between noon and 11:59 P.M.
    a/p Use the 12-hour clock and display a lowercase A with any hour before noon; display a lowercase P with any hour between noon and 11:59 P.M.
    AMPM Use the 12-hour clock and display the AM string literal as defined by your system with any hour before noon; display the PM string literal as defined by your system with any hour between noon and 11:59 P.M. AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by your system settings. The default format is AM/PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width