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
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

hungryanteater

New Member
Joined
Nov 22, 2007
Messages
13
second problem - nwo i have the new rows, is there an easy way to paste the values from the below cell?

cheers
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

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
 

hungryanteater

New Member
Joined
Nov 22, 2007
Messages
13

ADVERTISEMENT

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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

hungryanteater

New Member
Joined
Nov 22, 2007
Messages
13
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 :)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,511
Messages
5,596,581
Members
414,079
Latest member
Frills

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
Top