Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert Date error

Posted on 2012-03-12
6
162 Views
Last Modified: 2012-03-25
Please help fix this error, the script unable to convert the date after passing the midnight (00:00), I have highlighted this error in "yellow". By right the date at cell B8 should be updated with "13-Mar" and cell B21 with " 14-Mar".   Hope Experts able to fix this .
Sub ConvertData()
    With Sheet1
        For I = 2 To .UsedRange.Rows.Count
            If .Cells(I, 3) = "" Then
                Exit For
            End If
            
            .Cells(I, 1) = .Cells(I, 3) + 8 / 24
            
            If Hour(.Cells(I, 1)) < Hour(.Cells(I, 3)) Then
                .Cells(I, 2) = .Cells(I, 2) + 1
            End If
        Next I
    End With
End Sub

Open in new window

Time.xls
0
Comment
Question by:Billa7
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 37713137
If it is a zero value, i.e. has time value only and shows 12:00 AM, then just check for a zero value...

If .Cells(I, 3) = 0 Then

Of course, if you wanted a more precise time format, because you could be shaving minutes off by doing the course math manually, you could use actual time values ...

.Cells(i, 1) = .Cells(i, 3) + TimeValue("08:00")

HTH

Regards,
Zack Barresse
0
 

Author Comment

by:Billa7
ID: 37713347
Hi Zack Barresse,

I have tested by replacing this line:
 "If .Cells(I, 3) = 0 Then" and it's only update cell from A2:A7, other cells are remained empty.
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 37722509
Maybe closer to what you have then.  I'm not sure I understand the logic here or where the values are coming from...

Sub ConvertData_Updated()
    Dim WS As Worksheet
    Dim iLastRow As Long
    Dim i As Long
    Application.ScreenUpdating = False
    Set WS = ThisWorkbook.Worksheets("Data")
    iLastRow = WS.Range("C:C").Find(What:="*", After:=WS.Range("C1"), LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 2 To iLastRow Step 1
        WS.Cells(i, 1).Value = WS.Cells(i, 3).Value + TimeValue("08:00")
        If VBA.Hour(WS.Cells(i, 1).Value) < VBA.Hour(WS.Cells(i, 3).Value) Then
            WS.Cells(i, 2).Value = WS.Cells(i, 2).Value + 1
        End If
    Next i
    WS.Range("A2:A" & iLastRow).NumberFormat = "h:mm"
    WS.Range("B2:B" & iLastRow).NumberFormat = "dd-mmm-yyyy"
    WS.Range("C2:C" & iLastRow).NumberFormat = "h:mm"
    Application.ScreenUpdating = True
End Sub

HTH

Regards,
Zack Barresse
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Billa7
ID: 37723612
Hi Zack Barresse,

Sorry if request is really confusing you. The revised code still giving me a wrong date update. I've highlighted this error in yellow. Attached the workbook for your kind reference.
Time.xls
0
 
LVL 14

Accepted Solution

by:
Zack Barresse earned 500 total points
ID: 37725131
I *think* I see what you're talking about.  Try this revision, and if it's not giving you what you want, please give a more detailed description.  It appears that if the time is midnight and the updated time (+8) is after midnight (i.e. the next day) you want the date to move forward.  Is that correct?  Also, the dates basically are incrementing when you run this code by one day.  I'm not following the logic, so an explanation would help.

To get the day to increase if the above is the case, replace this line of code ...

If VBA.Hour(WS.Cells(i, 1).Value) < VBA.Hour(WS.Cells(i, 3).Value) Then

... with this line of code ...

If VBA.Hour(WS.Cells(i, 1).Value) < VBA.Hour(WS.Cells(i, 3).Value) Or WS.Cells(i, 3).Value = 0 Then

HTH

Regards,
Zack Barresse
0
 

Author Closing Comment

by:Billa7
ID: 37762510
Hi,

Thanks a lot for the help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to line 23 53
Excel if formula 2 19
finding cells where the formula results in no entry ("") and delete those cells using VBA 9 27
Excel change font color in formula 19 28
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question