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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
.
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,215,636
Messages
6,125,959
Members
449,276
Latest member
surendra75

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