+ Reply to Thread
Results 1 to 3 of 3

Problem with Date format when copied via VBA

  1. #1
    Registered User
    Join Date
    01-06-2019
    Location
    Switzerland
    MS-Off Ver
    1811
    Posts
    6

    Problem with Date format when copied via VBA

    Dear all

    I am currently working on a timetracking tool based on excel.

    There is also an option to archive current projects and also de-archive if needed. Further, the entries which have been archived are being analysed with a couple of functions which result in an overview report. E.g. looking for specific entries with date value > respectively < then a specific selected date ("=SUMIFS(Archiv!C:C;Archiv!B:B;">"&Woche!$S$8;Archiv!B:B;"<"&Woche!$S$9;Archiv!I:I;Woche!R18)".

    However, I have noticed, that the format for the date section in the archive is somehow not being realized by the above function, while the cells are being formated as "date" and not "general" or "text" or something. I have read that the date is not being recognized as date anymore when transferred via VBA, even if the format says "date" (weird?). Being said, when I manually reenter the dates in the respective archive column, the function recognizes the dates and works (even more weird?). I am new to excel and would really appreciate your help!

    My VBA code (1) fills a listbox with the current time entries - the relevant sections looks like this, where as the date part is marked bold:

    For i = 1 To 20
    If ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 31).Value = "RDY" Then
    UserForm41.ListBox1.AddItem i
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 1) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 25), "dd/mm/yyyy")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 2) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 26), "0.0")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 3) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 27), ">")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 4) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 32), "")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 5) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 33), "0000")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 6) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 28), "")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 7) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 29), "")
    UserForm41.ListBox1.List(UserForm41.ListBox1.ListCount - 1, 8) = Format(ThisWorkbook.Sheets("Zeiterfassung").Cells(2 + i, 30), "")
    End If
    Next i


    (2) the selected entries will be put to the archive. The section of the code looks like this:

    For n = 0 To UserForm41.ListBox1.ListCount - 1
    If UserForm41.ListBox1.Selected(n) = True Then
    'Einträge aus Listbox (selected) nach tab "Archiv" kopieren
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 2) = Format(UserForm41.ListBox1.List(n, 1), "dd/mm/yyyy")
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 3) = UserForm41.ListBox1.List(n, 2) 'EY Zeit
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 4) = UserForm41.ListBox1.List(n, 3) 'Kunde
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 5) = UserForm41.ListBox1.List(n, 4) 'E-Code
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 6) = Format(UserForm41.ListBox1.List(n, 5), "0000") 'Activity
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 7) = UserForm41.ListBox1.List(n, 6) 'Projekt
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 8) = UserForm41.ListBox1.List(n, 7) 'Projekt
    ThisWorkbook.Sheets("Archiv").Cells(ArchivNR, 9) = UserForm41.ListBox1.List(n, 8) 'Projekt

    End If
    Next


    For example, I have archived 2 entries (yellow):Capture.PNG
    The first entry was manually changed by myself (retyped the date into the cell) and is being recognized by my functions as date. The yellow ones are shown as date, but are not being recognized as such.

    Looking forward to your answers!

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Problem with Date format when copied via VBA

    When writing to the cell use CDate not Format

    Please Login or Register  to view this content.
    Rory

  3. #3
    Registered User
    Join Date
    01-06-2019
    Location
    Switzerland
    MS-Off Ver
    1811
    Posts
    6

    Re: Problem with Date format when copied via VBA

    Amazing! Thank you so much, it works perfectly fine now.

    Cheers, have a good one

+ 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. Date format problem
    By Joe Miller in forum Excel General
    Replies: 11
    Last Post: 04-19-2018, 08:04 AM
  2. Advanced filter by date range, international date format problem
    By Senator685 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-21-2013, 08:16 AM
  3. Date format problem
    By hpindoria in forum Excel General
    Replies: 9
    Last Post: 10-11-2012, 10:12 AM
  4. Date format problem
    By Pjcan1 in forum Excel General
    Replies: 5
    Last Post: 10-04-2012, 09:52 AM
  5. Date format not being copied?
    By Aland2929 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2012, 07:46 AM
  6. Date format problem
    By sagar in forum Excel General
    Replies: 7
    Last Post: 09-06-2007, 08:41 AM
  7. [SOLVED] Date Format problem
    By Mervyn Thomas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2005, 01:06 PM

Tags for this Thread

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