+ Reply to Thread
Results 1 to 9 of 9

date and time format together with quarter

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2010
    Location
    Sweden
    MS-Off Ver
    MS Office 365 Mac and Windows
    Posts
    94

    date and time format together with quarter

    Hello forum,

    I have output from a database and need to correct the "changed" column to correct format and also extract the year and its quarter.
    Column changed has "YYMMDDHHMM" the "0" disapears when open csv file in excel for years before 2010. I need to get full "YYYYMMDDHHMM" and also the year together with the quarter.

    Need to run this in a vba solution.
    Attached is some samples from the database. Column B and C is the output (wanted result)
    Attached Files Attached Files
    Last edited by soreno; 09-20-2018 at 07:01 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,744

    Re: date and time format together with quarter

    Here is the second part. Still working on the first part.

    Option Explicit
    
    Sub qtr()
        Dim i As Long, lr As Long
        Dim yr As String
        Dim qtr As String
        Application.ScreenUpdating = False
        lr = Range("B" & Rows.Count).End(xlUp).Row
        For i = 2 To lr
            yr = Left(Range("B" & i), 4)
            If Mid(Range("B" & i), 5, 2) = "01" Or Mid(Range("B" & i), 5, 2) = "02" Or Mid(Range("B" & i), 5, 2) = "03" Then
                qtr = "Q1"
            ElseIf Mid(Range("B" & i), 5, 2) = "04" Or Mid(Range("B" & i), 5, 2) = "05" Or Mid(Range("B" & i), 5, 2) = "06" Then
                qtr = "Q2"
            ElseIf Mid(Range("B" & i), 5, 2) = "07" Or Mid(Range("B" & i), 5, 2) = "08" Or Mid(Range("B" & i), 5, 2) = "09" Then
                qtr = "Q3"
            ElseIf Mid(Range("B" & i), 5, 2) = "10" Or Mid(Range("B" & i), 5, 2) = "11" Or Mid(Range("B" & i), 5, 2) = "12" Then
                qtr = "Q4"
            End If
            Range("C" & i) = yr & "-" & qtr
        Next i
        Application.ScreenUpdating = True
        MsgBox "Action completed"
    
    End Sub
    Last edited by alansidman; 09-20-2018 at 04:57 AM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,744

    Re: date and time format together with quarter

    Here's the first part

    Option Explicit
    
    Sub dtetm()
        Dim i As Long, lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Application.ScreenUpdating = False
        For i = 2 To lr
            If Len(Range("A" & i)) = 9 Then
                Range("B" & i) = 200 & Range("A" & i)
            Else: Range("B" & i) = 20 & Range("A" & i)
            End If
            Range("B2:B" & lr).NumberFormat = "0"
        Next i
        Application.ScreenUpdating = True
        MsgBox "completed"
    End Sub

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: date and time format together with quarter

    Sub Convert_Dates()
        Dim v, i&, q$
        With Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3)
            v = .Value
            For i = 1 To UBound(v, 1)
                v(i, 2) = Format(v(i, 1), "200000000000")
                Select Case Val(Mid(v(i, 2), 5, 2))
                    Case Is <= 3: q = "-Q1"
                    Case Is <= 6: q = "-Q2"
                    Case Is <= 9: q = "-Q3"
                    Case Is <= 12: q = "-Q4"
                End Select
                v(i, 3) = Left(v(i, 2), 4) & q
            Next
            .Value = v
        End With
        Columns("B").NumberFormat = "0"
    End Sub
    Last edited by AlphaFrog; 09-20-2018 at 05:21 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    03-08-2010
    Location
    Sweden
    MS-Off Ver
    MS Office 365 Mac and Windows
    Posts
    94

    Re: date and time format together with quarter

    @AlphaFrog,
    Excellent!! It works pretty fast too on my test of 900 000 rows , but if I have column "changed" at "F" and I want the output on "K" and "L", how would I change the code for that to work. I have a hard time to see where the actual output goes in your code...

    Thanks

  6. #6
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,177

    Re: date and time format together with quarter

    Or try:
    Sub test()
        Dim i As Long, lr As Long
        Dim sArr(), dArr
        lr = Range("B" & Rows.Count).End(xlUp).Row
        sArr = Range("A2:A" & lr).Value
        ReDim dArr(1 To UBound(sArr, 1), 1 To 2)
        
        For i = 1 To UBound(sArr, 1)
            dArr(i, 1) = CStr(200000000000# + sArr(i, 1))
            dArr(i, 2) = Left(dArr(i, 1), 4) & "-Q" & _
            Application.Ceiling(Mid(dArr(i, 1), 5, 2) / 3, 1)
        Next i
        Range("B2").Resize(i - 1, 2) = dArr
    End Sub

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: date and time format together with quarter

    You're welcome.

    Sub Convert_Dates()
        Dim v, i&, q$
        v = Range("F2", Range("F" & Rows.Count).End(xlUp)).Resize(, 2)
        For i = 1 To UBound(v, 1)
            v(i, 1) = Format(v(i, 1), "200000000000")
            Select Case Val(Mid(v(i, 1), 5, 2))
                Case Is <= 3: q = "-Q1"
                Case Is <= 6: q = "-Q2"
                Case Is <= 9: q = "-Q3"
                Case Is <= 12: q = "-Q4"
            End Select
            v(i, 2) = Left(v(i, 1), 4) & q
        Next
        Range("K2:L2").Resize(UBound(v, 1)).Value = v
        Columns("K").NumberFormat = "0"
    End Sub

  8. #8
    Registered User
    Join Date
    03-08-2010
    Location
    Sweden
    MS-Off Ver
    MS Office 365 Mac and Windows
    Posts
    94

    Re: date and time format together with quarter

    @AlphaFrog,
    Lovely, it crunch thru almost a million rows in less then 10 seconds, very impressive I applause and say thank you!

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: date and time format together with quarter

    You're welcome. Thanks for the feedback.

+ 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. [SOLVED] Converting Date & Time (General format) to Date (Date format)
    By supremenuts in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2017, 12:55 PM
  2. Custom Format Date, Quarter.
    By Xceller in forum Excel General
    Replies: 2
    Last Post: 02-05-2014, 06:00 PM
  3. Fiscal Quarter/Year into a date format
    By bluerog in forum Excel General
    Replies: 2
    Last Post: 12-14-2009, 02:36 PM
  4. Date Format - Quarter and Fiscal Year
    By bluerog in forum Excel General
    Replies: 1
    Last Post: 11-04-2009, 12:04 PM
  5. [SOLVED] Auto format quarter start date
    By mabeymom in forum Excel General
    Replies: 2
    Last Post: 07-14-2006, 02:42 PM
  6. [SOLVED] Format to display date as Quarter
    By Christine in forum Excel General
    Replies: 5
    Last Post: 10-18-2005, 11:05 PM
  7. Converting a date in Excel 2002 to a Year/Quarter format
    By Jim in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-10-2005, 04: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