Condense Dates

B1313

New Member
Joined
Sep 20, 2015
Messages
32
Hello,

I have a report that spits out various dates for individuals and they are all in one cell and look like the following:

"10/10, 10/11, 10/12, 10/13, 10/29, 10/30, 10/31, 11/1, 11/5, 11/6, 11/11, 11/30, 12/1, 12/5, 12/10, 12/24, 01/1, etc"

Now this is fine for a detailed report, but I need to condense the data dramatically as that current list could be anywhere from 1 to 120 dates and it is not practical to look at 120 dates individually. Is there a way (macro?) to make that list look like the following?

"10/10 - 10/13, 10/29 - 10/31, 11/1, 11/5 - 11/6, ..."

OR

"Oct (10 - 13, 29 - 31), Nov (1, 5 - 6, 11, 30), Dec (..." <-- Preferred

The second one is the most preferred display, but the first would work too. Ideally this will apply to hundreds of cells down thousands of rows.

Thanks,
 
Indeed. My data starts at G17, though that can shift.
You should not expect us to ever be able to guess that, so you should always tell us when you post your question.


Is it possible to make the solution dynamic to the range selected?
Assuming the data you want to process is surround by blanks (so that it does not "touch" any data that you do not want processed), select any single cell in the range you want processed and give this macro a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Data(R, C), " ", ""), ",", "/2000,") & ",", ",")
      If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
      Else
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
      End If
      Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
      Txt = ""
      LastItem = 0
      For X = 1 To UBound(Dtes)
        If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
          If X - LastItem = 1 Then
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
            LastItem = X
          Else
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
            LastItem = X
          End If
        End If
      Next
      Data(R, C) = Mid(Txt, 3)
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You should not expect us to ever be able to guess that, so you should always tell us when you post your question.



Assuming the data you want to process is surround by blanks (so that it does not "touch" any data that you do not want processed), select any single cell in the range you want processed and give this macro a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Data(R, C), " ", ""), ",", "/2000,") & ",", ",")
      If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
      Else
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
      End If
      Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
      Txt = ""
      LastItem = 0
      For X = 1 To UBound(Dtes)
        If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
          If X - LastItem = 1 Then
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
            LastItem = X
          Else
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
            LastItem = X
          End If
        End If
      Next
      Data(R, C) = Mid(Txt, 3)
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Apologies for that mistake. I tried that routine too and it throws the same thing and if I select a range then it says "mismatch". I would post a sample data set, but this site does not support attachments.

I have made dropbox link to a sample workbook: https://www.dropbox.com/s/8wispz7r6bl0178/Sample.xlsx?dl=0
 
Upvote 0
Apologies for that mistake. I tried that routine too and it throws the same thing and if I select a range then it says "mismatch". I would post a sample data set, but this site does not support attachments.

I have made dropbox link to a sample workbook: https://www.dropbox.com/s/8wispz7r6bl0178/Sample.xlsx?dl=0

From you first posted message in this thread...
I have a report that spits out various dates for individuals and they are all in one cell and look like the following:

"10/10, 10/11, 10/12, 10/13, 10/29, 10/30, 10/31, 11/1, 11/5, 11/6, 11/11, 11/30, 12/1, 12/5, 12/10, 12/24, 01/1, etc"

Now this is fine for a detailed report, but I need to condense the data dramatically as that current list could be anywhere from 1 to 120 dates and it is not practical to look at 120 dates individually. Is there a way (macro?) to make that list look like the following?

"10/10 - 10/13, 10/29 - 10/31, 11/1, 11/5 - 11/6, ..."

OR

"Oct (10 - 13, 29 - 31), Nov (1, 5 - 6, 11, 30), Dec (..." <-- Preferred




From the file you posted in your download link in Message #12...

ABCDEFGHIJK

<colgroup><col style="width: 30px; font-weight: bold;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"><col style="width: 64px;"></colgroup><tbody>
[TD="bgcolor: #CACACA, align: center"]1[/TD]
[TD="bgcolor: #000000, align: center"]Sizes:[/TD]
[TD="bgcolor: #000000, align: center"]Red[/TD]
[TD="bgcolor: #000000, align: center"]Blue[/TD]
[TD="bgcolor: #000000, align: center"]Green[/TD]
[TD="bgcolor: #000000, align: center"]Yellow[/TD]
[TD="bgcolor: #000000, align: center"]Black[/TD]
[TD="bgcolor: #000000, align: center"]White[/TD]
[TD="bgcolor: #000000, align: center"]Purple[/TD]
[TD="bgcolor: #000000, align: center"]Orange[/TD]
[TD="bgcolor: #000000, align: center"]Brown[/TD]

[TD="bgcolor: #CACACA, align: center"]2[/TD]
[TD="bgcolor: #969696, align: center"]Total[/TD]
[TD="bgcolor: #969696, align: center"]5[/TD]
[TD="bgcolor: #969696, align: center"]1[/TD]
[TD="bgcolor: #969696, align: center"]16[/TD]
[TD="bgcolor: #969696, align: center"]15[/TD]
[TD="bgcolor: #969696, align: center"]21[/TD]
[TD="bgcolor: #969696, align: center"]12[/TD]
[TD="bgcolor: #969696, align: center"]7[/TD]
[TD="bgcolor: #969696, align: center"]9[/TD]
[TD="bgcolor: #969696, align: center"]1[/TD]

[TD="bgcolor: #CACACA, align: center"]3[/TD]
[TD="align: center"]Item 1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]4[/TD]
[TD="align: center"]Item 2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]5[/TD]
[TD="align: center"]Item 3[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]6[/TD]
[TD="align: center"]Item 4[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]14[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"]11[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]

[TD="bgcolor: #CACACA, align: center"]7[/TD]
[TD="align: center"]Item 5[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]8[/TD]
[TD="align: center"]Item 6[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]9[/TD]
[TD="align: center"]Item 7[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="bgcolor: #CACACA, align: center"]10[/TD]
[TD="align: center"]Other[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

</tbody>




PLEASE EXPLAIN WHAT ONE HAS TO DO WITH THE OTHER!!!!!!!!
 
Last edited:
Upvote 0
WOW!! Epic fail, wrong file. I jumped the "dropbox sync time" gun Please cyber slap me. Here is the correct file

https://www.dropbox.com/s/0q6tjov7zla3gki/Sample V2.xlsx?dl=0
Consider yourself slapped. :grin:

The problem was the data in each cell ended with a comma which is the delimiter for the rest of the text, so the Split function produced an empty last element which my code was not anticipating. Here is modified code to get around the problem (it assumes each cell will always end with a comma)...
Code:
[table="width: 500"]
[tr]
	[td]Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Left(Data(R, C), Len(Data(R, C)) - 1), " ", ""), ",", "/2000,") & ",", ",")
      If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
      Else
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
      End If
      Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
      Txt = ""
      LastItem = 0
      For X = 1 To UBound(Dtes)
        If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
          If X - LastItem = 1 Then
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
            LastItem = X
          Else
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
            LastItem = X
          End If
        End If
      Next
      Data(R, C) = Mid(Txt, 3)
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Consider yourself slapped. :grin:

The problem was the data in each cell ended with a comma which is the delimiter for the rest of the text, so the Split function produced an empty last element which my code was not anticipating. Here is modified code to get around the problem (it assumes each cell will always end with a comma)...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Left(Data(R, C), Len(Data(R, C)) - 1), " ", ""), ",", "/2000,") & ",", ",")
      If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
      Else
        Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
      End If
      Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
      Txt = ""
      LastItem = 0
      For X = 1 To UBound(Dtes)
        If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
          If X - LastItem = 1 Then
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
            LastItem = X
          Else
            Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
            LastItem = X
          End If
        End If
      Next
      Data(R, C) = Mid(Txt, 3)
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

The code works perfectly!! You are an awesome person! Thank you profusely for all the help despite my blunders. Is there a rep system or someone for me to give you a thank you?

BTW, how did you learn so much about excel/excel's VBA? I want to learn how to develop these solutions to the MANY problems I face in my future.
 
Upvote 0
The code works perfectly!! You are an awesome person! Thank you profusely for all the help despite my blunders. Is there a rep system or someone for me to give you a thank you?
The only facility for that (that I am aware of) is to click the "Like" link in the lower right corner of any message that you found useful or interesting in some way.


BTW, how did you learn so much about excel/excel's VBA? I want to learn how to develop these solutions to the MANY problems I face in my future.
I have been programming (completely self-taught) in BASIC (the predecessor to the compiled version of VB), the compiled version of VB (which is a subset of VBA) and VBA for Excel since 1981 (not to mention a few other languages as well)... after all that time, you tend to develop certain programming styles and techniques that stay with you and programming itself becomes kind of automatic (I actually "think" in code when I see the problems posted here). So, stick with it and after about 35 years, you should be right where I am now (programming-wise).:laugh:
 
Last edited:
Upvote 0
The only facility for that (that I am aware of) is to click the "Like" link in the lower right corner of any message that you found useful or interesting in some way.



I have been programming (completely self-taught) in BASIC (the predecessor to the compiled version of VB), the compiled version of VB (which is a subset of VBA) and VBA for Excel since 1981 (not to mention a few other languages as well)... after all that time, you tend to develop certain programming styles and techniques that stay with you and programming itself becomes kind of automatic (I actually "think" in code when I see the problems posted here). So, stick with it and after about 35 years, you should be right where I am now (programming-wise).:laugh:

Yikes, I am seriously outgunned here. Though I am much more proficient with the formulas and end user Excel experience. I will definitely try to learn more though. I find a scenario where the script "breaks". If there is just one date in a cell (ie "01/04,") the same error is thrown. Since the code is more advanced than what I know, I did not try to tinker with it much for fear of breaking something.
 
Upvote 0
I find a scenario where the script "breaks". If there is just one date in a cell (ie "01/04,") the same error is thrown.
Sorry, I did not think you would have a single date in a cell given the length of your example from your first message. Here is a modified macro to get around that problem...
Code:
[table="width: 500"]
[tr]
	[td]Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Left(Data(R, C), Len(Data(R, C)) - 1), " ", ""), ",", "/2000,") & ",", ",")
      If UBound(Dtes) > 1 Then
        If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
          Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
        Else
          Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
        End If
        Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
        Txt = ""
        LastItem = 0
        For X = 1 To UBound(Dtes)
          If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
            If X - LastItem = 1 Then
              Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
              LastItem = X
            Else
              Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
              LastItem = X
            End If
          End If
        Next
        Data(R, C) = Mid(Txt, 3)
      End If
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I find a scenario where the script "breaks". If there is just one date in a cell (ie "01/04,") the same error is thrown.
Sorry, I did not think you would have a single date in a cell given the length of your example from your first message. Here is a modified macro to get around that problem...
[table="width: 500"]
[tr]
[td]
Code:
Sub CondenseDates()
  Dim R As Long, C As Long, X As Long, LastItem As Long, Data As Variant, Txt As String, Dtes() As String
  Data = ActiveCell.CurrentRegion
  For R = 1 To UBound(Data, 1)
    For C = 1 To UBound(Data, 2)
      Dtes = Split(Replace(Replace(Left(Data(R, C), Len(Data(R, C)) - 1), " ", ""), ",", "/2000,") & ",", ",")
      If UBound(Dtes) > 1 Then 
        If CDate(Left(Dtes(UBound(Dtes) - 2), Len(Dtes(UBound(Dtes) - 2)) - 5)) > CDate(Dtes(UBound(Dtes) - 1)) Then
          Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2001"
        Else
          Dtes(UBound(Dtes) - 1) = Dtes(UBound(Dtes) - 1) & "/2000"
        End If
        Dtes(UBound(Dtes)) = Dtes(UBound(Dtes) - 1)
        Txt = ""
        LastItem = 0
        For X = 1 To UBound(Dtes)
          If CDate(Dtes(X - 1)) + 1 <> CDate(Dtes(X)) Then
            If X - LastItem = 1 Then
              Txt = Txt & ", " & Format(Dtes(LastItem), "m/d")
              LastItem = X
            Else
              Txt = Txt & ", " & Format(Dtes(LastItem), "m/d") & "-" & Format(Dtes(X - 1), "m/d")
              LastItem = X
            End If
          End If
        Next
        Data(R, C) = Mid(Txt, 3)
      End If
    Next
  Next
  ActiveCell.CurrentRegion(1).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub
[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,752
Messages
6,168,007
Members
452,160
Latest member
Bekerinik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top