VBA To Split adjacent Cells into Rows

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
186
Hello. Very new to VBA (very new). I have a spreadsheet with three columns and need to do some work in the last two columns.

[Sheet 1].[Column A] - ID
[Sheet 1].[Column B] - BUILDING
[Sheet 1].[Column C] - DESC

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]ID
[/TD]
[TD="align: center"]BUILDING
[/TD]
[TD="align: center"]DESC
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]L1
L2
[/TD]
[TD]Very Large
Extended
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]L1
L3
L4
[/TD]
[TD]Extra Large
Tiny
Long
[/TD]
[/TR]
[TR]
[TD]C
[/TD]
[TD]S1
[/TD]
[TD]Small
[/TD]
[/TR]
[TR]
[TD]D
[/TD]
[TD]F3
F4
[/TD]
[TD]Fort
Fortable
[/TD]
[/TR]
</tbody>[/TABLE]

Columns B and C can contain multiple 'records' of data which are separated in the same cell by a return sign. For example, ID = A, the text "L1" and "L2" are separated by a paragraph symbol. The same goes for the text in Column C. "Very Large" and "Extended" are separated by a return symbol. The catch he is, the way this system reporting was designed, L1 is connected with Very Large, and L2 is connected with Extended, and L3 is connected with Tiny. So, basically the data which is connected to each other is the same just separated by a paragraph sign.

What I am trying to get VBA for, is to insert as many rows as need to split up the data in columns B and C; and then copy down the ID into the rows below it as well.

ANSWER:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]ID
[/TD]
[TD="align: center"]BUILDING
[/TD]
[TD="align: center"]DESC
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]L1
[/TD]
[TD]Very Large
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]L2
[/TD]
[TD]Extended
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]L1
[/TD]
[TD]Extra Large
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]L3
[/TD]
[TD]Tiny
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]L4
[/TD]
[TD]Long
[/TD]
[/TR]
[TR]
[TD]C
[/TD]
[TD]S1
[/TD]
[TD]Small
[/TD]
[/TR]
[TR]
[TD]D
[/TD]
[TD]F3
[/TD]
[TD]Fort
[/TD]
[/TR]
[TR]
[TD]D
[/TD]
[TD]F4
[/TD]
[TD]Fortable
[/TD]
[/TR]
</tbody>[/TABLE]
 
Rick,

I would really appreciate if you were able to assist in the code. I am stuck right now, and cannot do this manually - or I might throw my computer out the window.
 
Upvote 0

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
Still getting the run time error.
Is that error the same as you reported in Message #7 (error number 9, subscript out of range)? If not, what is the number and description?

What line of code is the error occurring on (that is, which line is highlighted in yellow when the code errors out)?
 
Upvote 0
It is the same error:

--------------------------
Run-time error '9':
Subscript out of range
--------------------------

After I click the run button, there is no highlighted line in the code as this error occurs.
 
Upvote 0
It is the same error:

--------------------------
Run-time error '9':
Subscript out of range
--------------------------

After I click the run button, there is no highlighted line in the code as this error occurs.
Does the error message box have a button labeled "Debug"? If so, click it... is one of the code lines highlighted now? If so, which one?
 
Upvote 0
Just a "OK" and a "HELP".
Can you post a copy of your workbook to DropBox so that the volunteers here can download it and test/debug our code out directly against your actual data? If you have sensitive information (names, addresses, phone numbers, email addresses, SS numbers, etc.), don't forget to replace them with junk text.
 
Upvote 0
I am not sure I can send this. There is too much PI on it.

I just ran it on 500 rows and it worked, but it did not work on 700 rows. Does that help at all?
 
Upvote 0
I am not sure I can send this. There is too much PI on it.
So are you saying the data that needs to be processed does not look at all like the example data you posted in Message #1 ? Is there private information on the sheet with the data that is being processed? If not, can you make a copy of your workbook and delete the worksheets that contain the private information... after doing that, does the code still work for 500 rows of data but not 700 rows of data? If so, then post the your original data with those worksheets deleted to DropBox for us. We only need the data that does not work (as long as it would work when the number of rows would be reduced).



I just ran it on 500 rows and it worked, but it did not work on 700 rows. Does that help at all?
No, without knowing what is in your cells and being able to see what the code is trying to do with those value live, I would have no idea where to start. There is something about your actual data that I seem unable to duplicate here.
 
Upvote 0
I think I might have figured it out!

So I found one row where Column B had three items; the but corresponding Column C only had 2 items. So, the last item in Column B would have a 'blank' in Column C after the de runs.

What I did was delete that row, and the code worked on the entire population. Is there a way to adjust the code to just put a blank in Column C instead of having to delete that record?
 
Upvote 0
I think I might have figured it out!

So I found one row where Column B had three items; the but corresponding Column C only had 2 items.
Yes, that would produce the error you were getting.



What I did was delete that row, and the code worked on the entire population. Is there a way to adjust the code to just put a blank in Column C instead of having to delete that record?
Give this revised code a try (it will tolerate missing information in either Column B or C). Note that is marks any blank cells in red so that you can see where there was a misalignment of data between the cell in Column B and the cell in Column C. If you do not want that functionality, simply remove the last line of code (above the End Sub statement).
Code:
[table="width: 500"]
[tr]
	[td]Sub ID_Building_Desc()
  Dim R As Long, X As Long, Z As Long, LastRow As Long, MaxNewRows As Long
  Dim MaxCol2 As Long, MaxCol3 As Long, ID As String
  Dim Data As Variant, Result As Variant, Bldg As Variant, Desc As Variant
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  Data = Range("A2:C" & LastRow).Value
  For R = 1 To UBound(Data)
    MaxCol2 = Len(Data(R, 2)) - Len(Replace(Data(R, 2), vbLf, ""))
    MaxCol3 = Len(Data(R, 3)) - Len(Replace(Data(R, 3), vbLf, ""))
    MaxNewRows = MaxNewRows + Application.Max(MaxCol2, MaxCol3) + 1
  Next
  ReDim Result(1 To MaxNewRows, 1 To 3)
  For R = 1 To UBound(Data)
    If Len(Data(R, 1)) > 0 And Data(R, 1) <> ID Then ID = Data(R, 1)
    Bldg = Split(Data(R, 2), vbLf)
    Desc = Split(Data(R, 3), vbLf)
    If UBound(Bldg) > UBound(Desc) Then
      ReDim Preserve Desc(0 To UBound(Bldg))
    ElseIf UBound(Bldg) < UBound(Desc) Then
      ReDim Preserve Bldg(0 To UBound(Desc))
    End If
    For Z = 0 To UBound(Bldg)
      X = X + 1
      Result(X, 1) = ID
      Result(X, 2) = Bldg(Z)
      Result(X, 3) = Desc(Z)
    Next
  Next
  Range("A2").Resize(UBound(Result), 3) = Result
  Range("B2:C" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlBlanks).Interior.Color = vbRed
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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