Results 1 to 9 of 9

Thread: VB control Excel date format

Threaded View

  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

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