+ Reply to Thread
Results 1 to 5 of 5

VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2016
    Location
    Bucharest
    MS-Off Ver
    2013
    Posts
    53

    VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

    Hi guys,

    I'm having a big excel file with multiple sheets; some of them contain "Time interval" column with UNIX epoch time.
    I'm running a macro to do the following:
    -search thru all sheets and where it finds the "Time interval" column,
    -applies the time conversion formula from UNIX to human time stamp, then
    -deletes the UNIX time column and keeps only the human time

    In sheet "General" $B20, I may have a GMT/UTC correction in minutes (e.q. -300 minutes > GMT -6hours).
    If this cell (B20) is different than 0, can you please help me to modify the code by introducing a new column (let's say- "Local time") that adjusts the human time stamp with the GMT offset of -300 minutes?
    Or, if much easier, just to update the formula with TIME function to add/subtract the offset of "General" sheet, B20, and add the output (human time stamp+GMT correction) in a single column.

    Thank you in advance

    The code:
    Sub ConvertDate()
      Dim i             As Long
      Dim wks           As Worksheet
      Dim rFind         As Range
      Dim rOut          As Range
    
     If Not Evaluate("ISREF(General!A1)") Then
          Exit Sub
     Else
      For i = 2 To ActiveWorkbook.Sheets.Count
        Set wks = Sheets(i)
    
        With wks
          Set rFind = .Cells.Find(What:="Time interval", LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)
          If Not rFind Is Nothing Then
            .Columns(rFind.Column + 1).EntireColumn.Insert
            Set rOut = Range(rFind, .Cells(Rows.Count, rFind.Column).End(xlUp)).Offset(, 1)
            If rOut.Rows.Count > 1 Then
              rOut.Cells(1) = "Time interval"
              With rOut.Offset(1)
                .NumberFormat = "dd/mm/yyyy hh:mm:ss"
                .FormulaR1C1 = "=IF(RC[-1] = """", """", RC[-1]/86400 + DATE(1970,1,1) - DATE(1900,1,1) + 1)"
                .Value2 = .Value2
                .EntireColumn.AutoFit
                .Offset(, -1).EntireColumn.Delete
              End With
            End If
          End If
        End With
      Next i
      End If
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,962

    Re: VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

        Dim TimeOffset    As Long
        
        If Not Evaluate("ISREF(General!A1)") Then
            Exit Sub
        Else
            With Sheets("GENERAL").Range("B20")
                TimeOffset = IIf(.Value = "", 0, .Value)
            End With
    
            For i = 2 To ActiveWorkbook.Sheets.Count
    .
    .
    .
                                .FormulaR1C1 = "=IF(RC[-1] = """", """", RC[-1]/86400 + " & TimeOffset & " + DATE(1970,1,1) - DATE(1900,1,1) + 1)"
    .
    .
    .
    .
    Last edited by protonLeah; 01-26-2019 at 09:19 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-09-2016
    Location
    Bucharest
    MS-Off Ver
    2013
    Posts
    53

    Re: VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

    Hello,

    The code works, the UNIX time is converted into human time but, the thing is that it takes the minutes from sheet "General".B20 and considers them as days.
    So, instead of adding/subtracting the offset to the time, it actually modifies the date so, instead of:
    "Jan-19 2019" the code returns "Mar-23 2018" (minus 300 days). The time is the kept unmodified.

    still playing with it ...
    Last edited by tuspilica; 01-27-2019 at 07:57 AM.

  4. #4
    Registered User
    Join Date
    02-09-2016
    Location
    Bucharest
    MS-Off Ver
    2013
    Posts
    53

    Re: VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

    divided the days to minutes and the formula works perfectly.

    Thank you for your help and time.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,962

    Re: VBA to adjsut the UNIX time stamp with GMT/UTC offset to local time

    You're welcome

+ 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. Convert Unix Time Stamp
    By leanheat in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-12-2016, 01:27 AM
  2. [SOLVED] EPOCH Time Conversion to local time and daylight savings time (DST)
    By cwwazy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-23-2015, 02:14 PM
  3. [SOLVED] Time Stamp Macro with Offset
    By rhapsody9 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2015, 05:25 PM
  4. [SOLVED] Convert Unix Date-time stamp to excel date and/or time columns
    By judikz in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-13-2015, 05:46 PM
  5. Replies: 3
    Last Post: 11-06-2012, 01:37 AM
  6. Date to Unix time stamp?
    By Newbie in forum Excel General
    Replies: 2
    Last Post: 07-20-2005, 08:05 PM
  7. [SOLVED] Excel Date Time to Unix Time
    By jnorton in forum Excel General
    Replies: 2
    Last Post: 05-24-2005, 02: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