Insert two empty lines only in the "N:T" columns

harzer

Board Regular
Joined
Dec 15, 2021
Messages
153
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
I have a group of columns "N:T" which contains a variable number of rows, I want to add two empty lines after each row in this group of columns starting from cell "N2" and ending at the last row of the of column “T”.
Data outside of the "N:T" columns should not be modified.
I would like to ask you for a solution with arrays (if it is possible) because there are a large number of lines to process.

Sheet before inserting the two empty lines

Permuter2Ranges.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1abcdefghijklmJeunePèreMèreJeunePèreMère
2abcdefghijklmNM96-010/2021 FNM96-002/2020 MNM96-020/2020 FNM96-008/2021 MNM96-002/2020 MNM96-020/2020 Fnopq
3abcdefghijklmAE27-026/2022 FNM96-046/2019 MNM96-036/2021 FAE27-018/2022 MNM96-005/2021 MAE27-068/2021 Fnopq
4abcdefghijklmAE27-037/2022 FNM96-005/2021 MAE27-068/2021 FAE27-019/2022 MNM96-005/2021 MAE27-068/2021 Fnopq
Feuil1


Result after inserting the two empty lines

Permuter2Ranges.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXY
1abcdefghijklmJeunePèreMèreJeunePèreMère
2abcdefghijklmNM96-010/2021 FNM96-002/2020 MNM96-020/2020 FNM96-008/2021 MNM96-002/2020 MNM96-020/2020 Fnopqrstwxyz
3abcdefghijklmnopqrstwxyz
4abcdefghijklmnopqrstwxyz
5AE27-026/2022 FNM96-046/2019 MNM96-036/2021 FAE27-018/2022 MNM96-005/2021 MAE27-068/2021 F
6
7
8AE27-037/2022 FNM96-005/2021 MAE27-068/2021 FAE27-019/2022 MNM96-005/2021 MAE27-068/2021 F
9
10
11
Feuil1


Thank you in advance for your contributions.
 
You are welcome.
Glad we were able to help you to get to a working solution!
:)
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
because there are a large number of lines to process.
We didn't ever get an indication of what "large number" means to you, but you may want to consider alternatives.

For inserting the extra lines ..
VBA Code:
Sub Insert_Rows_v2()
  Dim a As Variant
  Dim i As Long, j As Long, k As Long, rws As Long

  Application.ScreenUpdating = False
  Columns("U").Insert
  rws = Columns("N:T").Find("*", , , , xlByRows, xlPrevious).Row - 1
  ReDim a(1 To 3 * rws, 1 To 1)
  For i = 1 To 3
    For j = 1 To rws
      k = k + 1: a(k, 1) = j
    Next j
  Next i
  Range("U2").Resize(UBound(a)).Value = a
  Range("N2:U2").Resize(UBound(a)).Sort Key1:=Columns("U"), Order1:=xlAscending, Header:=xlNo
  Columns("U").Delete
  Application.ScreenUpdating = True
End Sub

With about 5,000 rows of data, on my (old) machine, the code from post 16 took 20.387 seconds. The above code took the code took 0.059 seconds. That is an improvement of about 350x

To remove those same inserted cells (that is from rows 3,4,6,7,9,10 etc) ..
VBA Code:
Sub Delete_Rows()
  Dim lr As Long
   
  Application.ScreenUpdating = False
  Columns("U").Insert
  lr = Columns("N:T").Find("*", , , , xlByRows, xlPrevious).Row
  With Range("N2:U" & lr)
    .Columns(.Columns.Count).Value = Evaluate("if(mod(row(" & .Address & "),3)=2,1,"""")")
    .Sort Key1:=Columns("U"), Order1:=xlAscending, Header:=xlNo
  End With
  Columns("U").Delete
  Application.ScreenUpdating = True
End Sub

For this process, code from post 17 for me took 37.883 seconds and this code took 0.068 seconds, about 550x faster.
Note that my code does not check that the inserted cells are still empty as that requirement was not entirely clear to me. The check could be added to my code if required.
 
Upvote 0
Solution
Hello Peter_SSs,
Thank you for your feedback and the solutions proposed.
I admit that when it comes to speed, it’s exceptional.
1. Concerning the code for inserting empty lines (on 6000 lines initially): Execution time is 0.23 sec.
2. Regarding the code for removing empty lines:
Before inserting the lines, I had 6000 lines.
After inserting the lines I have 17996 lines) --> Execution time to delete empty lines is 0.21 sec.
Thanks Peter_SSs for this code.
 
Upvote 0
You're welcome. Glad it helped. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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