Rows (i).Insert

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the below Macro to insert a row between groups of customers. Is it
possible to specify the row height of the inserted row, without changing the
height of the other rows?

Dim lastrow As Long, i As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) <> Cells(i - 1, 1) Then
Rows(i).Insert.RowHeight = 15
End If
Next
 
Hi Josh,

Try:

'=============>>
Public Sub Tester()
Dim lastrow As Long, i As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If Cells(i, 1) <> Cells(i - 1, 1) Then
Rows(i).Insert
Rows(i).RowHeight = 15
End If
Next
End Sub
'<<=============
 
Hi Josh,

To avoid potential problems, it is always advisable
to be explicit.

Therefore, better would be:

'=============>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet

Set WB = ThisWorkbook '<<=== CHANGE
Set SH = WB.Sheets("Sheet3") '<<=== CHANGE
Dim lastrow As Long, i As Long

With SH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 2 Step -1
If .Cells(i, 1).value <> Cells(i - 1, 1).value Then
.Rows(i).Insert
.Rows(i).RowHeight = 15
End If
Next i
End With
End Sub
'<<=============
 

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

Back
Top