Splitting multiple row cells into new rows

cspearsall

New Member
Joined
May 23, 2016
Messages
19
Imaging - ready to work.xlsx
ABCDE
1Procedure NameModality TypeProcedure CodeCPT
2BI BREAST CYST ASPIRATION LEFTBreast ImagingIMG61719000 19000 3611900001PR PUNCTURE ASPIRATION CYST BREAST PR PUNCTURE ASPIRATION CYST BREAST HC PUNC/ASPIR BREAST CYST
3BI BREAST CYST ASPIRATION RIGHTBreast ImagingIMG195219000 19000 3611900001PR PUNCTURE ASPIRATION CYST BREAST PR PUNCTURE ASPIRATION CYST BREAST HC PUNC/ASPIR BREAST CYST
4BI BREAST DUCTOGRAM LEFTBreast ImagingIMG57977053 77053 4017705301CHG MAMMARY DUCTOGRAM OR GALACTOGRAM SINGLE CHG MAMMARY DUCTOGRAM OR GALACTOGRAM SINGLE HC MAMMARY DUCTOGRAM, SINGLE - MAMMO BREAST DUCTOGRAM
Epic - fill in (2)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:G1,K1,I1Cell ValueduplicatestextYES



I need to find a way to make each of these matching rows in columns D & E into a separate row with the same information from columns A,B&C
 

Attachments

  • Excel Example.png
    Excel Example.png
    84.3 KB · Views: 2
Should Alan be too busy to design your Power Query M code ... can always suggest a tailor-made macro ... ;)

By the way, regarding CPT - Column D - are all cells structured the very same way : 5 characters , 5 characters, 10 characters with char(10) in between ?
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I was thinking of:
For each row in the sheet:
1. counting the carriage returns in row D (Variable x)
2. looping 1 to X
A. using find to get the ending index of the first row (then save as a variable)
B. using the find with the previous variable +1 to find the index of the end of the second row, etc..
C. Right and Left functions to extract the row contents
D. Then copying it to another sheet in column D with the contents of columns A.B&C

But I am a little stuck in the weeds on how to make it happen


Haven't used power query but looking now

@James006 - no the numbers are not uniform in length

@James006 & @alansidman - this is not an uncommon practice for us to do and if there was a macro I could really speed up my process with the next client
 
Upvote 0
Are you tempted to dive into designing your own macro ...

If the answer is "Yes" ... Turn on your macro recorder and perform all your tasks for a single row : Row 2

You will have a pretty good start with an initial VBA translation ...

which, then... will only need to be streamlined and included in a Loop to handle your 1'300 records !!!
 
Upvote 0
Yea, I have done some Macro work before and that is usually my method. But I think the hang up is the moving the data to a new sheet. I've started trying to do now....stay tuned. I'm not saying a power query or some help with the macro would be helpful--- ;)
 
Upvote 0
Feel free, once you have it, to post the backbone of your macro ...

By the way, "moving data to another sheet" boils down to a standard Copy Paste ...
 
Upvote 0
A quick example to Explode Row 2 ...
VBA Code:
Sub ExplodeRow2()
' Quick Example for Row 2
Dim cpt, pro
Dim j As Long
    Sheet1.Range("A1:E1").Copy Sheet2.Range("A1:E1")
    Sheet1.Range("A2:C2").Copy Sheet2.Range("A2:A4"
    Sheet1.Range("D2:E2").Copy Sheet2.Range("D2:E2")
    cpt = Split(Sheet2.Range("D2"), Chr(10))
    For j = LBound(cpt) To UBound(cpt)
        Sheet2.Cells(j + 2, 4) = cpt(j)
    Next j
    pro = Split(Sheet2.Range("E2"), Chr(10))
    For j = LBound(pro) To UBound(pro)
        Sheet2.Cells(j + 2, 5) = pro(j)
    Next j
End Sub

Hope this will help ;)
 
Upvote 0
VBA Code:
Sub Macro2()

' Add an output sheet
Set MainSheet1 = ActiveSheet
MainSheet = ActiveSheet.Name
NumROws = Range("A2", Range("A2").End(xlDown)).Rows.Count
Range("A2").Select
Sheets.Add.Name = "OutputSheet"

[B]For rw = 1 To NumROws[/B]
    Worksheets(MainSheet).Activate
    ProcNameValue = ActiveCell.Offset(0, 0).Value
    ModalityValue = ActiveCell.Offset(0, 1).Value
    ProcCodeValue = ActiveCell.Offset(0, 2).Value
    CPTCellValue = ActiveCell.Offset(0, 3).Value
    CPTArray = Split(CPTCellValue, Chr(10))
    LineCount = UBound(CPTArray)
    DescriptCellValue = ActiveCell.Offset(0, 4).Value
    DescriptArray = Split(DescriptCellValue, Chr(10))

Worksheets("OutputSheet").Activate
    For I = 0 To LineCount
        Range("A" & Rows.Count).End(xlUp).Offset(1).Select
        ActiveCell.Offset(0, 0).Value = ProcNameValue
        ActiveCell.Offset(0, 1).Value = ModalityValue
        ActiveCell.Offset(0, 2).Value = ProcCodeValue
        ActiveCell.Offset(0, 3).Value = CPTArray(I)
        ActiveCell.Offset(0, 4).Value = DescriptArray(I)
    Next I

[B]Next[/B]

End Sub


Its not properly notated yet, nut here it my very simple problem and I'm sure I am missing something small:
1. This iterates back to the same row each time. I can add something that will move the active cell down one at the start of each loop put I know there is a better way to iterate through the rows...any help

The array stuff just came to me and works very well
 
Upvote 0
I have a working code but I would like to tweak it to make it a lot faster. My Macro switches back and forth between sheets to read and write. Is there a way to do this without visually switching? This seems to slow things way down. Also, I think there is a more elegant solution to iterating through rows than the clunky method I used.

VBA Code:
Sub ImagingParse()

' Add an output sheet
Set MainSheet1 = ActiveSheet
MainSheet = ActiveSheet.Name
NumROws = Range("A2", Range("A2").End(xlDown)).Rows.Count
Range("A1").Select
Sheets.Add.Name = "OutputSheet"

For rw = 1 To NumROws - 1200
    Worksheets(MainSheet).Activate
    ActiveCell.Offset(1).Select
    ProcNameValue = ActiveCell.Offset(0, 0).Value
    ModalityValue = ActiveCell.Offset(0, 1).Value
    ProcCodeValue = ActiveCell.Offset(0, 2).Value
    CPTCellValue = ActiveCell.Offset(0, 3).Value
    CPTArray = Split(CPTCellValue, Chr(10))
    LineCount = UBound(CPTArray)
    DescriptCellValue = ActiveCell.Offset(0, 4).Value
    DescriptArray = Split(DescriptCellValue, Chr(10))
    ' Msg = CPTArray(0) & DescriptArray(0)

Worksheets("OutputSheet").Activate
    For I = 0 To LineCount
        Range("A" & Rows.Count).End(xlUp).Offset(1).Select
        ActiveCell.Offset(0, 0).Value = ProcNameValue
        ActiveCell.Offset(0, 1).Value = ModalityValue
        ActiveCell.Offset(0, 2).Value = ProcCodeValue
        ActiveCell.Offset(0, 3).Value = CPTArray(I)
        ActiveCell.Offset(0, 4).Value = DescriptArray(I)
    Next I

Next

End Sub
 
Upvote 0
Is this what you are expecting for results.

Procedure NameModality TypeProcedure CodeCPTColumn1
BI BREAST CYST ASPIRATION LEFTBreast ImagingIMG61719000PR PUNCTURE ASPIRATION CYST BREAST
BI BREAST CYST ASPIRATION LEFTBreast ImagingIMG61719000HC PUNC/ASPIR BREAST CYST
BI BREAST CYST ASPIRATION LEFTBreast ImagingIMG6173611900001PR PUNCTURE ASPIRATION CYST BREAST
BI BREAST CYST ASPIRATION LEFTBreast ImagingIMG6173611900001HC PUNC/ASPIR BREAST CYST
BI BREAST CYST ASPIRATION RIGHTBreast ImagingIMG195219000PR PUNCTURE ASPIRATION CYST BREAST
BI BREAST CYST ASPIRATION RIGHTBreast ImagingIMG195219000HC PUNC/ASPIR BREAST CYST
BI BREAST CYST ASPIRATION RIGHTBreast ImagingIMG19523611900001PR PUNCTURE ASPIRATION CYST BREAST
BI BREAST CYST ASPIRATION RIGHTBreast ImagingIMG19523611900001HC PUNC/ASPIR BREAST CYST
BI BREAST DUCTOGRAM LEFTBreast ImagingIMG57977053CHG MAMMARY DUCTOGRAM OR GALACTOGRAM SINGLE
BI BREAST DUCTOGRAM LEFTBreast ImagingIMG57977053HC MAMMARY DUCTOGRAM, SINGLE - MAMMO BREAST DUCTOGRAM
BI BREAST DUCTOGRAM LEFTBreast ImagingIMG5794017705301CHG MAMMARY DUCTOGRAM OR GALACTOGRAM SINGLE
BI BREAST DUCTOGRAM LEFTBreast ImagingIMG5794017705301HC MAMMARY DUCTOGRAM, SINGLE - MAMMO BREAST DUCTOGRAM
 
Upvote 0
Hi again,

Below is a macro to be tested ... should Explode All Rows as expected ;)
VBA Code:
Sub ExplodeAllRows()
Dim cpt, pro
Dim j As Long, i As Long, x As Long
Dim lastr1 As Long, last2 As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    lastr1 = Sheet1.Cells(Rows.count, 1).End(xlUp).row
    Sheet1.Range("A1:E1").Copy Sheet2.Range("A1:E1")
    For i = 2 To lastr1
        last2 = Sheet2.Cells(Rows.count, 1).End(xlUp).row + 1
        Sheet1.Range("A" & i & ":C" & i).Copy Sheet2.Range("A" & last2 & ":A" & last2 + 2)
        Sheet1.Range("D" & i & ":E" & i).Copy Sheet2.Range("D" & last2 & ":E" & last2 + 2)
    Next i
    x = Sheet2.Cells(Rows.count, 4).End(xlUp).row
    For i = 2 To x Step 3
        cpt = Split(Sheet2.Range("D" & i), Chr(10))
        For j = LBound(cpt) To UBound(cpt)
            Sheet2.Cells(i + j, 4) = cpt(j)
        Next j
        pro = Split(Sheet2.Range("E" & i), Chr(10))
        For j = LBound(pro) To UBound(pro)
            Sheet2.Cells(i + j, 5) = pro(j)
        Next j
    Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

Hope this will help :)
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,132
Members
449,097
Latest member
mlckr

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