Code Explanation

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
The code below inserts a row between data which is <> identical to the preceding cell in column A.

What I am unsure of is the lastrow to 3. In particular 3. Could someone provide explanation and or can this be observed line by line.

Thank You

Still learning

Sub insertRows()


Dim i As Integer
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row


For i = lastRow To 3 Step -1

If Range("A" & i).Value <> Range("A" & i - 1) Then
Rows(i).Resize(1).Insert

End If

Next




End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
.
If you mean ... can you see it function one line at a time ...

Code:
Sub insertRows()


Dim i As Integer
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row


For i = lastRow To 3 Step -1


If Range("A" & i).Value <> Range("A" & i - 1) Then
Rows(i).Resize(1).Insert


End If

MsgBox ""


Next
End Sub


This line :
Code:
For i = lastRow To 3 Step -1
causes the macro to start at the "lastRow" (last used row in Col A), go up the rows, one row at a time (Step - 1) until it reaches row #3 (To 3).
 
Last edited:
Upvote 0
.
Go to the VBE, place your cursor at the beginning of the macro code, press F8. Will execute macro one line at a time.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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