Alter this code to insert 3 blank rows?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Good afternoon, stumped on this one. Here is the code that I have so far, but have not been able to add the missing piece.

VBA Code:
Sub InsertBlank()
Dim i As Long
Dim LR As Long

LR = Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To LR
        If Range("A" & i).Value = "NYC" Then Range("A" & i).EntireRow.Insert

    Next
End Sub

Unsure of what I need to change here. I'm trying to insert 3 rows when Range("A" & i).value ="NYC"

Any ideas?
 
Not sure starting a new thread for the same thing will be appreciated by moderators, but I'm not one so I don't know. Anyway, what I last suggested won't work because once a For Next loop has begun, the start, end, step parameters cannot be changed (by a global variable) SO the solution is to use a Do While loop, as in
VBA Code:
Sub InsertBlank()
Dim i As Long, n As Long, LR As Long

'NoEvents = True
LR = Range("A" & Rows.count).End(xlUp).Row

i = 2
Do While i < LR + 1
  Debug.Print Range("A" & i)
  If Range("A" & i) = "NYC" Then
    For n = 1 To 3
       Range("A" & i).EntireRow.Insert
       LR = LR + 1
       i = i + 1
    Next
  End If
  i = i + 1
Loop
'NoEvents = False

End Sub
I have other module code that that code will cause to run, which I don't want. So in a standard module (a code module that does not belong to a sheet) where I have
Option Explicit
I added
Public noevents As Boolean
You could try the above code with noevents commented out, put the cursor in the code somewhere and press F8 to initiate, and repeatedly press again to watch the flow. If you find that when inserting a row that this code branches out to some other procedure, you should note the names of any of those procedures as well as determine what the implications are if you allow that procedure to continue. If that happens and you want to stop it, insert a line at the beginning like
If noevents Then Exit Sub (or Exit Function).

You should comment out the debug.print line when you are finished testing.
 
Last edited:
Upvote 0
Solution

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Not sure starting a new thread for the same thing will be appreciated by moderators, but I'm not one so I don't know. Anyway, what I last suggested won't work because once a For Next loop has begun, the start, end, step parameters cannot be changed (by a global variable) SO the solution is to use a Do While loop, as in
VBA Code:
Sub InsertBlank()
Dim i As Long, n As Long, LR As Long

'NoEvents = True
LR = Range("A" & Rows.count).End(xlUp).Row

i = 2
Do While i < LR + 1
  Debug.Print Range("A" & i)
  If Range("A" & i) = "NYC" Then
    For n = 1 To 3
       Range("A" & i).EntireRow.Insert
       LR = LR + 1
       i = i + 1
    Next
  End If
  i = i + 1
Loop
'NoEvents = False

End Sub
I have other module code that that code will cause to run, which I don't want. So in a standard module (a code module that does not belong to a sheet) where I have
Option Explicit
I added
Public noevents As Boolean
You could try the above code with noevents commented out, put the cursor in the code somewhere and press F8 to initiate, and repeatedly press again to watch the flow. If you find that when inserting a row that this code branches out to some other procedure, you should note the names of any of those procedures as well as determine what the implications are if you allow that procedure to continue. If that happens and you want to stop it, insert a line at the beginning like
If noevents Then Exit Sub (or Exit Function).

You should comment out the debug.print line when you are finished testing.
Thanks Micron. Sorry, I meant to say that I would start a new thread in which I’ll try to split the invoices without the need of inserting blank rows. Did not mean to say that I would be reposting this one.

I’ll give this code a try shortly and report back.

Many thanks!
 
Upvote 0
Not sure starting a new thread for the same thing will be appreciated by moderators, but I'm not one so I don't know. Anyway, what I last suggested won't work because once a For Next loop has begun, the start, end, step parameters cannot be changed (by a global variable) SO the solution is to use a Do While loop, as in
VBA Code:
Sub InsertBlank()
Dim i As Long, n As Long, LR As Long

'NoEvents = True
LR = Range("A" & Rows.count).End(xlUp).Row

i = 2
Do While i < LR + 1
  Debug.Print Range("A" & i)
  If Range("A" & i) = "NYC" Then
    For n = 1 To 3
       Range("A" & i).EntireRow.Insert
       LR = LR + 1
       i = i + 1
    Next
  End If
  i = i + 1
Loop
'NoEvents = False

End Sub
I have other module code that that code will cause to run, which I don't want. So in a standard module (a code module that does not belong to a sheet) where I have
Option Explicit
I added
Public noevents As Boolean
You could try the above code with noevents commented out, put the cursor in the code somewhere and press F8 to initiate, and repeatedly press again to watch the flow. If you find that when inserting a row that this code branches out to some other procedure, you should note the names of any of those procedures as well as determine what the implications are if you allow that procedure to continue. If that happens and you want to stop it, insert a line at the beginning like
If noevents Then Exit Sub (or Exit Function).

You should comment out the debug.print line when you are finished testing.
This one works!!!!

Thank you so much
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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