1

Topic: Manipulating vague dates in Excel

Has anyone written a function for Excel to manipulate vague dates? We have some data to import shared to us via the Gateway and the date format is always in three fields

Date start | Date end | Date type
01/01/2014 | 31/01/2014 | Month
07/10/2012 | 07/10/2012 | Day
01/01/1968 | 31/12/1968 | Year

(plus other formats such as day ranges, month ranges, year ranges etc.)

To import this requires some manipulation as the R6 import wizard to my knowledge (and please correct me if I'm wrong) only allows import from a single date field, although it stores three equivalent fields in the back-end database. Whilst the process to sort this in Excel is usually fairly quick with simple formulas and filters, I wondered if anyone had written an Excel VBA function or formula to deal with this in a more automated fashion?

A function like the following with three arguments would make life very simple:

=CONVERTVAGUEDATE(date_start,date_end,date_type)

Just a thought... no point re-inventing the wheel if someone has done something along these lines already.

MARK

Mark Pollitt
SWSEIC (formerly DGERC)

2 (edited by RobLarge 30-09-2014 15:33:04)

Re: Manipulating vague dates in Excel

I have a VBA function, designed for MS Access rather than Excel, but it should be reasonably easy to modify for Excel. Might even work as is. I think I merely recoded the stored procedure which R6 uses in VBA rather than T-SQL. The code is belo9w.

Now that I look at it though, it converts from R6 vague date to a string, so the input parameters are two long integers and a string rather than two dates and a string. So you will need another function to convert from your dates into R6 vague date integers.

Which makes me think, if you convert your start and end dates into strings and concatenate them with a hyphen between e.g. "01/01/2011 - 31/12/2011" the R6 import wizard ought to be able to process this.



Public Function BuildRecorderDateString(ByVal VagueDateStart, ByVal VagueDateEnd As Long, ByVal VagueDateType As String) As String

    Dim MM1, MM2, RP1, RDS1, RDS2, RMS1, RMS2, VD As String
    Dim Z, G, H, A, B, Y, C, M, D, RD, Y4, RD1, RM1, RY1, RD2, RM2, RY2, X, CC1, CC2 As Long
   
    X = 0
    Do While X < 2
        VD = VagueDateType
        RD = VagueDateStart + 693594
        If X > 0 Then RD = VagueDateEnd + 693594
        Z = RD + 306
        H = 100 * Z - 25
        A = Int(H / 3652425)
        B = A - Int(A / 4)
        Y = Int((100 * B + H) / 36525)
        C = B + Z - 365 * Y - Int(Y / 4)
        M = Int((5 * C + 456) / 153)
        G = Int((153 * M - 457) / 5)
        D = C - G
        If M > 12 Then
            Y = Y + 1
            M = M - 12
        End If
   
        If X = 0 Then
            RD1 = D
            RM1 = M
            RY1 = Y
            CC1 = Int(Y / 100) + 1
   
            If RD1 < 10 Then
                RDS1 = "0" & Trim(Str(RD1))
            Else
                RDS1 = Trim(Str(RD1))
            End If
            If RM1 < 10 Then
                RMS1 = "0" + Trim(Str(RM1))
            Else
                RMS1 = Trim(Str(RM1))
            End If
        Else
            RD2 = D
            RM2 = M
            RY2 = Y
            CC2 = Int(Y / 100)
            If RD2 < 10 Then
                RDS2 = "0" + Trim(Str(RD2))
            Else
                RDS2 = Trim(Str(RD2))
            End If
            If RM2 < 10 Then
                RMS2 = "0" & Trim(Str(RM2))
            Else
                RMS2 = Trim(Str(RM2))
            End If
        End If
        X = X + 1
    Loop
    Select Case RM1
        Case 1
            MM1 = "January"
        Case 2
            MM1 = "February"
        Case 3
            MM1 = "March"
        Case 4
            MM1 = "April"
        Case 5
            MM1 = "May"
        Case 6
            MM1 = "June"
        Case 7
            MM1 = "July"
        Case 8
            MM1 = "August"
        Case 9
            MM1 = "September"
        Case 10
            MM1 = "October"
        Case 11
            MM1 = "November"
        Case 12
            MM1 = "December"
    End Select
    Select Case RM2
        Case 1
            MM2 = "January"
        Case 2
            MM2 = "February"
        Case 3
            MM2 = "March"
        Case 4
            MM2 = "April"
        Case 5
            MM2 = "May"
        Case 6
            MM2 = "June"
        Case 7
            MM2 = "July"
        Case 8
            MM2 = "August"
        Case 9
            MM2 = "September"
        Case 10
            MM2 = "October"
        Case 11
            MM2 = "November"
        Case 12
            MM2 = "December"
    End Select
       
    RP1 = Trim(Str(RD1)) & "/" & Trim(Str(RM1)) & "/" & Trim(Str(RY1))
   
    Select Case VD
        Case "D"
            RP1 = RDS1 & "/" & RMS1 & "/" & Trim(Str(RY1))
        Case "DD"
            RP1 = RDS1 & "/" & RMS1 & "/" & Trim(Str(RY1)) & " - " & RDS2 & "/" & RMS2 & "/" & Trim(Str(RY2))
        Case "O"
            RP1 = MM1 & "/" & Trim(Str(RY1))
        Case "OO"
            RP1 = MM1 & "/" & Trim(Str(RY1)) & " - " & MM2 & " " & Trim(Str(RY2))
        Case "Y"
            RP1 = Trim(Str(RY1))
        Case "YY"
            RP1 = Trim(Str(RY1)) & " - " & Trim(Str(RY2))
        Case "-Y"
            RP1 = " - " & Trim(Str(RY2))
        Case "Y-"
            RP1 = Trim(Str(RY1)) & " - "
        Case "C"
            RP1 = Trim(Str(CC1)) & "c"
        Case "CC"
            RP1 = Trim(Str(CC1)) & "c - " & Trim(Str(CC2)) & "c"
        Case "-C"
            RP1 = " - " + Trim(Str(CC2)) & "c"
        Case "C-"
            RP1 = Trim(Str(CC2)) & "c  -  "
        Case "U"
            RP1 = "Unknown"
    End Select
               
    If VD = "P" Or VD = "S" Then
        If RM2 = 2 Then RP1 = "Winter"
        If RM2 = 8 Then RP1 = "Summer"
        If RM2 = 11 Then RP1 = "Autumn"
        If RM2 = 5 Then RP1 = "Spring"
        If VD = "P" Then RP1 = RP1 & " " & Trim(Str(RY2))
    End If
   
    BuildRecorderDateString = RP1
   
End Function

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

3

Re: Manipulating vague dates in Excel

Thanks Rob

Will have a look at your code example.

Simple concatenation of the start and end dates does work and import into R6. Unfortunately all dates get set to a vague_date_type of day range (DD) in R6, so a date of 01/01/2012-31/12/2012 goes in as a day range rather than a year. Not a big issue, but a bit untidy when it comes to reporting.

Cheers

MARK

Mark Pollitt
SWSEIC (formerly DGERC)

4

Re: Manipulating vague dates in Excel

Hi Mark

I suspected that might happen. OK then, perhaps when importing you could identify an unused field which can be imported (such as sample comment) and where the type is D, M or Y, populate that field with something which you can use later to  filter with. Then it should be easy enough to create a batch update which changes just the vague date type field.

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

5

Re: Manipulating vague dates in Excel

Just experimenting with this custom function and it seems to work on initial testing. It assumes the data you're dealing with is clean and has already been validated (e.g an export from R6 or NBN gateway) and is in three fields - vague date start, vague date end and date type. Bases the conversions on the date type value and doesn't do any checking for validity or otherwise of the dates themselves.  Only coded to format most likely data types encountered (day, month year, day range, month range, year range, before year and season) - any others will produce an error message. I'm sure someone could code this much more efficiently - might be useful to others. Feel free to improve on it/correct it if you spot any obvious errors!!

MARK

Public Function VDtoR6date(VDstart As Date, VDend As Date, VDtype As String) As String

Dim newdate As String
Dim VDsm As String

VDsm = Format(VDstart, "MM")

Select Case VDtype

Case "Day", "D"
newdate = Format(VDstart, "DD/MM/YYYY")
Case "Year", "Y"
newdate = Format(VDstart, "YYYY")
Case "Month", "O"
newdate = Format(VDstart, "MMM YYYY")
Case "Day Range", "DD"
newdate = Format(VDstart, "DD/MM/YYYY") & " - " & Format(VDend, "DD/MM/YYYY")
Case "Month Range", "OO"
newdate = Format(VDstart, "MMM YYYY") & " - " & Format(VDend, "MMM YYYY")
Case "Year Range", "YY"
newdate = Format(VDstart, "YYYY") & " - " & Format(VDend, "YYYY")
Case "Season", "P"
    If VDsm = "03" Then
    newdate = "Spring " & Format(VDstart, "YYYY")
    ElseIf VDsm = "06" Then
    newdate = "Summer " & Format(VDstart, "YYYY")
    ElseIf VDsm = "09" Then
    newdate = "Autumn " & Format(VDstart, "YYYY")
    ElseIf VDsm = "12" Then
    newdate = "Winter " & Format(VDstart, "YYYY")
    Else: newdate = "Error in conversion - please convert manually"
    End If
Case "Before Year", "-Y"
newdate = "-" & Format(VDend, "YYYY")
Case "No date", "U"
newdate = "UNKNOWN"
Case Else
newdate = "Error in conversion - please convert manually"
End Select

VDtoR6date = newdate

End Function
Mark Pollitt
SWSEIC (formerly DGERC)

6

Re: Manipulating vague dates in Excel

Is there an easy answer to import Start and End dates into R6. We are getting a lot of datsa with just these fields and as R6 exports them why can't it import them.

7 (edited by RobLarge 16-10-2014 13:51:55)

Re: Manipulating vague dates in Excel

As I said above, concatenate the two dates with a hyphen and they will import fine, but if you want months to appear as months etc. you will need a batch update to change vague_date_type.

Shouldn't be too hard to code, but I have never had the need to.

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre