Hello all,
I have built a macro which formats a sheet of data to meet a specification. It works fine, but I have recently encountered the need to format some data differently. I was hoping to keep my old macro and then build another that could be run afterwards to add some final formatting if desired. Basically I want records to be grouped if groups exist. The data has two levels of customers in each row. In the event that two or more rows consecutively have the same higher level of customer, I want to insert a blank row above the first, and another blank row below the last.
Here is my code to add a blank row below the last:
Now this works exactly as desired. However, it is adding the blank row above the first that is proving to be a struggle for me. I've tried the opposite of the above if statement:
Which results in an infinite loop as the new row above shifts the row with focus down one, then the next row to be evaluated becomes the row that has just been moved, which is still the same as the row below it, and so another row is inserted and then the same row evaluated again, and so on.
Is there a way to move to the next cell plus one? I was thinking that perhaps I could create a new variable that the first part of my if statement could set to one value and the 'elseif' part could set to another. Then I could put an if statement around the 'Next' command that says if the variable equals the first value then 'Next', elseif it equals the second, move to 'Next + 1'. Or something like that. Is it possible? If not, then what can I do? It doesn't need to be fancy, it just needs to work!
Thanks,
Matthew
I have built a macro which formats a sheet of data to meet a specification. It works fine, but I have recently encountered the need to format some data differently. I was hoping to keep my old macro and then build another that could be run afterwards to add some final formatting if desired. Basically I want records to be grouped if groups exist. The data has two levels of customers in each row. In the event that two or more rows consecutively have the same higher level of customer, I want to insert a blank row above the first, and another blank row below the last.
Here is my code to add a blank row below the last:
Code:
Sub NEWTEST()
Dim rnArea As Range
Dim rnCell As Range
Set rnArea = Range("C8:C1000")
For Each rnCell In rnArea
With rnCell
If rnCell.Text = rnCell.Offset(-1).Text And rnCell.Text <> rnCell.Offset(1).Text Then
rnCell.Select
ActiveCell.Offset(1).EntireRow.Insert
End If
End With
Next
End Sub
Code:
ElseIf rnCell.Text <> rnCell.Offset(-1, 0).Text And rnCell.Text = rnCell.Offset(1, 0).Text Then
rnCell.EntireRow.Insert shift:=xlDown
Is there a way to move to the next cell plus one? I was thinking that perhaps I could create a new variable that the first part of my if statement could set to one value and the 'elseif' part could set to another. Then I could put an if statement around the 'Next' command that says if the variable equals the first value then 'Next', elseif it equals the second, move to 'Next + 1'. Or something like that. Is it possible? If not, then what can I do? It doesn't need to be fancy, it just needs to work!
Thanks,
Matthew