How to split information from cell into multiple rows

davidplowman

New Member
Joined
Oct 31, 2013
Messages
10
Hello:

I have an issue and I'm hoping someone can help me with.

I have a spreadsheet that's has 28 columns and more than 3,000 rows.

One of the columns is a year range (i.e. 2010-2013, 1988-1999, 1996-1998 or whatever).

The file has to be submitted to another department for an upload to a website, and each year has to be listed on a separate row, with all of the information remaining the same.

So in other words, a portion of my spreadsheet that looks like this:</SPAN>
Source</SPAN></SPAN>
New PN (Main # to Search By)</SPAN></SPAN>
Make</SPAN></SPAN>
Vehicle Type</SPAN></SPAN>
Year (From - To)</SPAN></SPAN>
Model</SPAN></SPAN>
Engine</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2010-2013</SPAN></SPAN>
DB</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2012-2013</SPAN></SPAN>
DB</SPAN></SPAN>
HA</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
1999-2003</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>

<TBODY>
</TBODY>

Would ultimately look like this:</SPAN>

Source</SPAN></SPAN>
New PN (Main # to Search By)</SPAN></SPAN>
Make</SPAN></SPAN>
Vehicle Type</SPAN></SPAN>
Year (From - To)</SPAN></SPAN>
Model</SPAN></SPAN>
Engine</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2010</SPAN></SPAN>
DB</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2011</SPAN></SPAN>
DB</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2013</SPAN></SPAN>
DB</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2012</SPAN></SPAN>
DB</SPAN></SPAN>
HA</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2013</SPAN></SPAN>
DB</SPAN></SPAN>
HA</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
1999</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2000</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2001</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2002</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>
Lester</SPAN></SPAN>
A7T03277A</SPAN></SPAN>
Hyster</SPAN></SPAN>
Lift Truck</SPAN></SPAN>
2003</SPAN></SPAN>
Various Models</SPAN></SPAN>
FE</SPAN></SPAN>

<TBODY>
</TBODY>


The problem is I’m not sure of the best way to get from Point A to Point B. I would image in would involve splitting the beginning and end year into end rows, then calculating the year range (3 years, 1 year and 4 years.)</SPAN>

But I’m unsure of what to do from there? Is it just a process of manually inserting the rows and copying in the information from the other columns, or is there a quicker, more efficient way of doing this?</SPAN>

As a side note, I’m familiar with many of the “advanced” functions of Excel, but haven’t done any VBA program yet. But if there is a program that would help, I’m willing to take the plunge into learning about it.</SPAN>
Thanks,</SPAN>
David</SPAN>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Give this macro a try (see note at end for manual changes you have to make)...

Rich (BB code):
Sub SplitDataByYears()
  Dim R As Long, C As Long, CC As Long, Index As Long, TotalYears As Long, LastRow As Long
  Dim YearCol As Long, ArrIn As Variant, ArrOut As Variant
  Const YearLetter As String = "E"
  YearCol = Cells(1, YearLetter).Column
  LastRow = Cells(Rows.Count, YearCol).End(xlUp).Row
  ArrIn = Range("A1:AB" & LastRow)
  TotalYears = 1 + Evaluate("SUMPRODUCT(ISNUMBER(FIND(""-""," & YearLetter & "2:" & YearLetter & _
                            LastRow & "))*(1+RIGHT(" & YearLetter & "2:" & YearLetter & LastRow & _
                            ",4)-LEFT(" & YearLetter & "2:" & YearLetter & LastRow & ",4)))")
  ReDim ArrOut(1 To TotalYears, 1 To 28)
  For R = 2 To UBound(ArrIn)
    For CC = CLng(Left(ArrIn(R, YearCol), 4)) To CLng(Right(ArrIn(R, YearCol), 4))
      Index = Index + 1
      For C = 1 To 28
        If C = YearCol Then
          ArrOut(Index, C) = CC
        Else
          ArrOut(Index, C) = ArrIn(R, C)
        End If
      Next
    Next
  Next
  With Sheets("Sheet2")
    .Range("A1:AB1") = ActiveSheet.Range("A1:AB1").Value
    .Range("A2:AB" & UBound(ArrOut) + 1) = ArrOut
  End With
End Sub

NOTE: Change my assumed Column E for the year range, highlighted in red, to the actual column letter containing the years. Also change the output sheet name as needed (highlighted in green)... if you want to overwrite the original data with the newly formatted data, then change Sheets("Sheet2") to ActiveSheet instead.
 
Upvote 0
Rick:


Thank you so much for your help with this and your extremely quick response!

In reviewing my data, I realize I'll have some "prep work" in getting the data in the date range to where it needs to be, but I'm looking forward to trying out my first code out, and seeing how it works... I just know this will save me so much time as opposed to doing it manually.

I will definitely report back once I've gotten my data in order and have had a chance to try this out!

Thanks so much for your help!

Thanks again,
David</SPAN>
 
Upvote 0
In reviewing my data, I realize I'll have some "prep work" in getting the data in the date range to where it needs to be, but I'm looking forward to trying out my first code out, and seeing how it works... I just know this will save me so much time as opposed to doing it manually.

What do you mean by "getting the data in teh date range where it needs to be"? It can stay in the column it is in now, just change the letter "E" that I highlighted in red to the letter for the column that those dates are really in... the rest of the code will automatically adjust to that change once you do that.
 
Upvote 0
Rick: </SPAN>

I was just referring to the incompleteness my data in my long spreadshee--especially in the date range section (the example I posted was “idealized” to make my question simpler).

I've been downloading data from various sources, and my source material in the date range is incomplete and inconsistent in a lot of cases.... Some rows have just one year listed, others have no year at all, others just say "n/a" etc. Both for the sake completeness of my spreadsheet and for the macro to work, I’m sure I will have to have product specialists review all of the lines and fill in a complete year range.

I'm sure the program you provided will work great; I just meant I will have I'll need to do some inernat "data consistency work" so I can be at a point where I can put it to proper use.. (Otherwise, it would be the old “garbage in, garbage out” scenario, no matter how helpful this macro is…) Oh well, such has been the nature of this project, and is I suppose "job security" for me!</SPAN>
At any rate, I’m sure this will be a huge help!</SPAN>

Thanks again,</SPAN>
David</SPAN>
 
Upvote 0
Some rows have just one year listed, others have no year at all, others just say "n/a" etc.
</SPAN>
Just so you know, the code I posted will work for both single years and for year ranges (but as you guessed, not for blank, N/A, etc.).
 
Upvote 0
Rick:

That's great to know! I was wondering about how single-year info would work with the program... Again I appreciate your help!

Thanks,
David
 
Upvote 0
Hey All:

The scope of the project changed, I'm just being able to get to this.

I haven't worked for VBA before, so I'm hoping my question isn't too rudimentary.

But in trying to run the above VBA script, I get an "Expected line or statement or end of Statement" error. What am I doing wrong? How do I fix this?

Also, so I don't have to keep asking basic questions, where should I go to get a "crash course" on VBA programming?

Thank you,
David</SPAN>
 
Upvote 0
Hello:

Just an update, I figured it out, and everything works wonderfully!!!!

Thanks so much for your help!

Thanks,
David
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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