adding rows conditionally

hungryanteater

New Member
Joined
Nov 22, 2007
Messages
13
hi all

i have an exported spreadsheet that i need to convert to link into our financial package

it has 8 columns and variable rows depending on the export.

one column has customer name which changes every few rows (although not a set amount) - i need to be able to identify where the customer name has changes and insert 2 rows before the change

cant seem to find any answers or macro code to do it - any help most welcome!

thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try like this (assumes customer is in column A)

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Resize(2).Insert
Next i
End Sub
 
Upvote 0
Does this do what you want?

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    Rows(i).Copy
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Resize(2).Insert
Next i
End Sub
 
Upvote 0
ok, maybe i cant iron out the last few issues.

the rows that have been inserted and copied from the row below, how can i only select the first 3 columns within the row to be copied and the rest remaining as blank

help is most gratefully appreciated
 
Upvote 0
I think this will work - try it with a copy of your sheet

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    Rows(i).Copy
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then
        Rows(i).Resize(2).Insert
        Range("D" & i + 1).Resize(2, 5).ClearContents
    End If
Next i
End Sub
 
Upvote 0
thanks again. works like a dream.

question (hopefully the last). im exporting from another source and placing the export into this spreadsheet where I run the macro and it then pulls certain data through to a second sheet. however when i run the macro (has an addtional bit of sort code as well) it jumbles the order of the second sheet.

any idea how to prevent this?

thanks for all your help :)
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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