Macro to insert a line when a value changes in a range


N

nowhoffman

Hi I am new to using macros in Excel, but I was curious if someone could give me some advice. I am looking for a macro that will insert a blank row whenever 2 values in the same column don't match. For example in range b5:b500 I have several different dates, and two make the spreadsheet look better when I print it, I would like there to be a blank row between every time the value is different between two cells. Any help would be very much appreciated. Thanks.
 
Ad

Advertisements

C

Claus Busch

Hi,

Am Tue, 11 Mar 2014 23:19:04 -0700 (PDT) schrieb (e-mail address removed):
Hi I am new to using macros in Excel, but I was curious if someone could give me some advice. I am looking for a macro that will insert a blank row whenever 2 values in the same column don't match. For example in range b5:b500 I have several different dates, and two make the spreadsheet look better when I print it, I would like there to be a blank row between every time the value is different between two cells. Any help would be very much appreciated. Thanks.

try:
Sub InsertRows()
Dim LRow As Long
Dim i As Long

With ActiveSheet
LRow = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = LRow To 5 Step -1
If .Cells(i, 2) <> .Cells(i - 1, 2) Then
Rows(i).Insert
End If
Next
End With
End Sub


Regards
Claus B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top