UDF or standard IF function for date range?

Travis Cowart

I am trying to create a range from numbers in a column, and create a range. For example,

column F: 
1899 
1912 
1915 
1918 
1929 
1934 
1935 
1936
... 

So as soon as 1934, 1935, 1936 sequence starts, it would capture 1934 and iterate through until the difference isn't 1.

The result for this example would be:

1934 - 1936. 

Once I create these date ranges, I would use text to columns by the "-" delimiter to create begin and end date columns.

The difficulty in this is comparing another column and making sure the dates are based on a particular title.

So: IF(Title 1 = Title 2, (IF date 2 - date 1 = 1, increment to check date 3 - date 2 = 1, otherwise = date 1)).

I could try to nest many IF statements, but that would get nasty, plus no idea how many dates to compare per title.

Iteration seems like it would be much easier by creating a sub or UDF in VBA, but I haven't found any examples on here (or elsewhere) that may be helpful.

Oligg

This will do the job, it will take cells in column F and print your output in column G. If there is a range of years (ex: 1934-1935-1936), it will put it in a single cell as a string.

Sub range_dates()
Dim cel As Range
Dim iRow As Long, n As Long
Dim title As String, nextTitle As String
iRow = 2
n = 0
For i = 2 To 15
    Set cel = Cells(i, "F")
    title = Cells(i, "E").Value
    nextTitle = Cells(i + 1, "E").Value
    diff = Cells(i + 1, "F").Value - cel.Value

    If diff = 1 And title = nextTitle Then
        firstDate = cel.Value - n
        n = n + 1
    Else
        Cells(iRow, "H").Value = cel.Value
        Cells(iRow, "G").Value = title
        If n > 0 Then
            Cells(iRow, "H") = "'" & firstDate & " - " & cel.Value
            n = 0
        End If
        iRow = iRow + 1
    End If
Next
End Sub

I do know this code is not the most optimized, but as of now it's the one i have to share. Feel free to improve it :)

EDIT: I added the title conditions and changed the output to prevent excel from changing the string to a date format

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PYSPARK UDF to explode records based on date range

MongoDB MapReduce Standard Deviation over date range

openERP function for validate date range

Date range custom function on the current date

Declare variable with date function in standard SQL bigquery

Date function in standard SQL in Google bigquery

Non standard evaluation of date condition with filter function

Create a non standard weekly date range autofill in excel

Custom month range with current date in window function

BigQuery - Issue with TABLE_DATE_RANGE() function

vectorization of date range function for transformation of pandas dataframe

Javascript - Sort date range array function

Standard function to find element that produces maximum output across a range?

Using standard evaluation with a udf in dplyr

Hive UDF with date incrementing

How do I make Excel user-defined function (UDF) accept arguments of String type and range type?

Passing two date in a Function using udf getting error of df.show() in pyspark (similar to apply function in pandas)

Transform a range and return it from a UDF

How to pass String and Range in UDF

Is it possible to schedule queries in Google BigQuery using standard SQL such that date range increments everyday while it runs?

How to extract a list of indices based on a date range and Category match in Excel? [Ver: Standard 2019]

use dict of function in UDF

Delphi UDF function to C

UDF and python function in Pyspark

Undefined function UDF in pyspark?

Scala and Spark UDF function

function gives wrong total of a week day between a date range in django

Excel: Check If date range exist in the column Using IF and Count function?

Show all data in a date range using MYSQL recursive function