Results 1 to 24 of 24

Thread: [RESOLVED] Date in Excel

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Resolved [RESOLVED] Date in Excel

    Hi

    I tried to use code below, but when the data array is date , no work fine

    Code:
    Dim lMyArray(2, 1) As Longl
    MyArray(0, 0) = format(now,"dd/mm/yyyy")
    MyArray(0, 1) = format(now +1,"dd/mm/yyyy")
    MyArray(1, 0) = format(now +2,"dd/mm/yyyy")
    MyArray(1, 1) = format(now +3"dd/mm/yyyy")
    MyArray(2, 0) = format(now +4,"dd/mm/yyyy")
    MyArray(2, 1) = format(now +5,"dd/mm/yyyy")
    
    
    XLSheet.Range("D7:E9").Value = lMyArray
    In Excel date show all column as "0/1/1900"
    Why ?

    I see code in http://www.vbforums.com/showthread.php?t=391665 for other data
    work fine

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Date in Excel

    Try this

    vb Code:
    1. Sub Sample()
    2.     Dim MyArray(2, 1) As Date
    3.     MyArray(0, 0) = Format(Now, "dd/mm/yyyy")
    4.     MyArray(0, 1) = Format(Now + 1, "dd/mm/yyyy")
    5.     MyArray(1, 0) = Format(Now + 2, "dd/mm/yyyy")
    6.     MyArray(1, 1) = Format(Now + 3, "dd/mm/yyyy")
    7.     MyArray(2, 0) = Format(Now + 4, "dd/mm/yyyy")
    8.     MyArray(2, 1) = Format(Now + 5, "dd/mm/yyyy")
    9.     XLSheet.Range("D7:E9").Value = MyArray
    10.     XLSheet.Range("D7:E9").NumberFormat = "dd/mm/yyyy"
    11. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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

    Re: Date in Excel

    formatted dates are strings not longs
    myarray is not lmyarray
    you may need to format the workshee cells for that range

    i tested this appeared correct
    vb Code:
    1. Dim lMyArray(2, 1) As Variant
    2. lMyArray(0, 0) = Format(Now, "dd/mm/yyyy")
    3. lMyArray(0, 1) = Format(Now + 1, "dd/mm/yyyy")
    4. lMyArray(1, 0) = Format(Now + 2, "dd/mm/yyyy")
    5. lMyArray(1, 1) = Format(Now + 3, "dd/mm/yyyy")
    6. lMyArray(2, 0) = Format(Now + 4, "dd/mm/yyyy")
    7. lMyArray(2, 1) = Format(Now + 5, "dd/mm/yyyy")
    8. Range("D7:E9").Value = lMyArray
    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

  4. #4

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    sorry, in my code are Date

    Code:
    Dim lMyArray(2, 1) As date

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Date in Excel

    So if your query is solved then do remember to mark the thread resolved

    Check the link in my signature on how to resolve it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Thank, but the problem is not resolved


  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    If they are supposed to be Dates then put dates into them, not Strings (which is what Format returns). eg:
    Code:
    Dim lMyArray(2, 1) As Date
    lMyArray(0, 0) = now
    lMyArray(0, 1) = now +1
    lMyArray(1, 0) = now +2
    lMyArray(1, 1) = now +3
    lMyArray(2, 0) = now +4
    lMyArray(2, 1) = now +5
    
    XLSheet.Range("D7:E9").Value = lMyArray
    This will ensure that the correct values are passed, but they might not display properly - if that is the case, change the format of the cells in Excel (and record a macro so that you can see the code to do it).
    Last edited by si_the_geek; Jul 25th, 2009 at 06:13 AM. Reason: fixed mistake in copy+paste!

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

    Re: Date in Excel

    si copied the original code

    turn option explicit on, it may resolve some of your problems
    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

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    oops, fixed.


    Here's an FAQ article on what westconn1 mentioned: What is Option Explicit, and why should I use it?

  10. #10

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Thank

    Already with
    vb Code:
    1. Option Explicit

  11. #11

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Quote Originally Posted by si_the_geek View Post
    If they are supposed to be Dates then put dates into them, not Strings (which is what Format returns). eg:
    Code:
    Dim lMyArray(2, 1) As Date
    lMyArray(0, 0) = now
    lMyArray(0, 1) = now +1
    lMyArray(1, 0) = now +2
    lMyArray(1, 1) = now +3
    lMyArray(2, 0) = now +4
    lMyArray(2, 1) = now +5
    
    XLSheet.Range("D7:E9").Value = lMyArray
    This will ensure that the correct values are passed, but they might not display properly - if that is the case, change the format of the cells in Excel (and record a macro so that you can see the code to do it).

    Thank , but I tried from VB and inside Excel the code below, and no work
    Code:
    Sub insere_datas()
       Dim Dt_Expedicao()   As Date
       Dim i As Integer
        ReDim Dt_Expedicao(0)
      Do While i < 9
        ReDim Preserve Dt_Expedicao(UBound(Dt_Expedicao) + 1)
        Dt_Expedicao(UBound(Dt_Expedicao)) = Now()     'CStr(rs!data_expedicao)
        i = i + 1
      Loop
      '   Range("AF4:AF12").Select
        Range("AF4:AF12").NumberFormat = "@"
        Range("AF4:AF12").Value = Dt_Expedicao
        Range("AF4:AF12").NumberFormat = "dd/mm/yyyy"
        Range("AF4:AF12").HorizontalAlignment = xlCenter
      
      
       
    End Sub
    Show me as 00/01/1900


  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    That is because:

    1) You are using a one-dimensional array (which goes across the sheet, and gets copied downwards if apt) rather than a two-dimensional array (which goes across and down).

    2) You do not set a value for the first array element, so it is the default value.

  13. #13

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Sorry, I did not understand

  14. #14

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Thank , eg when I tried code below It put only same date

    vb Code:
    1. Dim lMyArray(2) As Date
    2. lMyArray(0) = Now
    3. lMyArray(1) = Now + 1
    4. lMyArray(2) = Now + 2
    5. Plan1.Range("D7:D9").Value = lMyArray

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    You are using a one dimensional array:
    Code:
    Dim lMyArray(2) As Date
    ..it needs to be a two-dimensional array (as it was in post #7 and earlier):
    Code:
    Dim lMyArray(2, 1) As Date

  16. #16

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Thank You

    But I must put only a column , in Left column and right column already data and can not to changed


    tia

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    So?

    There is nothing to stop you using a 2-dimensional array with just one column (eg: Dim lMyArray(2, 0) As Date )

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

    Re: Date in Excel

    you can use a 1 dimensional array like
    vb Code:
    1. Dim lMyArray(2) As Date
    2.       lMyArray(0) = Now
    3.       lMyArray(1) = Now + 1
    4.       lMyArray(2) = Now + 2
    5.       Range("D7:D9").Value = Application.WorksheetFunction.Transpose(lMyArray)
    6.       Range("d7:d9").NumberFormat = "dd/mm/yy"
    the transpose function swaps row and columns
    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

  19. #19

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Unhappy Re: Date in Excel

    I tried to use your code , but put values (dates ) inconsistent

    Code:
    Dim lMyArray(3) As Variant
    
    'ReDim Preserve lMyArray(0, 2) As Variant
    
    lMyArray(0) = Now
    lMyArray(1) = Now + 1
    lMyArray(2) = Now + 2
    Plan1.Range("D7:D9").Value = Application.WorksheetFunction.Transpose(Dt_Expedicao)
    Plan1.Range("d7:d9").NumberFormat = "dd/mm/yy"
    Show me

    04/08/09
    00/01/00
    00/01/00

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

    Re: Date in Excel

    what is dt_expedicao
    it is suppossed to be the array (lMyarray) with the dates that is transposed

    i tested and works correctly

    what is your local date format?
    you could try changing to
    Plan1.Range("d7:d9").NumberFormatLocal = "dd/mm/yy"
    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

  21. #21

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Red face Re: Date in Excel

    Quote Originally Posted by si_the_geek View Post
    So?

    There is nothing to stop you using a 2-dimensional array with just one column (eg: Dim lMyArray(2, 0) As Date )
    Thank

    Your Idea work fine , But I am using a loop with recordset and need resize array for each record

    Code:
      Do While Not rs.EOF
          
            'Acrescenta uma linha no array
           
            Dt_Expedicao(UBound(Dt_Expedicao)) = Now()     'CStr(rs!data_expedicao)]
            ReDim Preserve Dt_Expedicao(UBound(Dt_Expedicao) + 1) As Date
    
       loop
    If I need put in two dimension I can to use Redim Preserve in second dimension

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    As it is coming from a recordset (assuming data_expedicao is the only field in it), there is no need to use an array or any loops - you can simply use CopyFromRecordset instead:
    Code:
    Plan1.Range("D7").CopyFromRecordset rs

  23. #23

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Date in Excel

    Sorry, variable error, Your solution woked fine !!!!

    How can I to post like resolved ?

  24. #24
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Date in Excel

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

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