VBA help to insert rows etc

philcart

Board Regular
Joined
Feb 12, 2003
Messages
177
Hi,
I’d appreciate guidance please, on coding the following in an Excel 2007 workbook –
While there is data in col B, work down col K starting from K2
If K2 is blank, move to K3, and so on
When you come to a nonblank cell in col K, let’s use K5 as an example, check the LEN
If Len(K5) = 11, then insert one blank row under row 5, and copy values from cells C5 to J5 into the newly inserted C6 to J6. Copy K5 into B6
If LEN(K5) = 23, then insert TWO blank rows under row 5, and copy values from cells C5 to J5 into the newly inserted C6 to J6, and C7 to J7. Copy LEFT(K5,11) into B6, and Copy RIGHT(K5,11) into B7
If LEN(K5) = 35, then insert THREE blank rows under row 5, and copy values from cells C5 to J5 into the newly inserted C6 to J6, C7 to J7, and C8 to J8. Copy LEFT(K5,11) into B6, copy MID(K5,13,11) into B7 and copy RIGHT(K5,11) into B8.
As ever, all help gratefully received.
Cheers,
Phil
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This should work. Try it out on a copy of your workbook
Code:
Sub Philcart()
 
    Dim lr As Long, i As Long
 
    lr = Range("B" & Rows.Count).End(xlUp).Row
 
    For i = lr To 2 Step -1
        Select Case Len(Range("K" & i).Value)
            Case 11
                Rows(i + 1).Insert
                With Range("C" & i + 1 & ":J" & i + 1)
                    .Formula = "=C" & i
                    .Value = .Value
                End With
                Range("B" & i + 1).Value = Range("K" & i).Value
            Case 23
                Rows(i + 1 & ":" & i + 2).Insert
                With Range("C" & i + 1 & ":J" & i + 2)
                    .Formula = "=C" & i
                    .Value = .Value
                End With
                Range("B" & i + 1).Value = Left(Range("K" & i).Value, 11)
                Range("B" & i + 2).Value = Right(Range("K" & i).Value, 11)
            Case 35
                Rows(i + 1 & ":" & i + 3).Insert
                With Range("C" & i + 1 & ":J" & i + 3)
                    .Formula = "=C" & i
                    .Value = .Value
                End With
                Range("B" & i + 1).Value = Left(Range("K" & i).Value, 11)
                Range("B" & i + 2).Value = Mid(Range("K" & i).Value, 13, 11)
                Range("B" & i + 3).Value = Right(Range("K" & i).Value, 11)
        End Select
    Next i
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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