Hello everyone,
I found the following code which allows me to insert an empty line each time the value of my cell in column "B" changes, of course, this code does the job but only given the size of the data, it takes a while to run, how can we modify it to optimize it, or even replace it with code that uses arrays (UBound) so that it is faster.
Thanks in advance for your suggestions.
Unsorted array :
Unless I am mistaken, here is the table sorted on column "B" with rows inserted :
I found the following code which allows me to insert an empty line each time the value of my cell in column "B" changes, of course, this code does the job but only given the size of the data, it takes a while to run, how can we modify it to optimize it, or even replace it with code that uses arrays (UBound) so that it is faster.
Thanks in advance for your suggestions.
VBA Code:
Sub Insert_Rows()
'''Trier le tableau (A2:L & derniere) ligne sur la colonne "B" avant d'inserer les lignes
Range("A2:L" & Range("B" & Rows.Count).End(xlUp).Row).Sort _
key1:=Range("B2"), order1:=xlAscending, Header:=xlNo
Dim ws As Worksheet, x As Long, LastRow As Long
Set ws = ActiveSheet
With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For x = LastRow - 1 To 2 Step -1
If .Cells(x, "B").Value <> .Cells(x + 1, "B").Value Then
.Rows(x + 1).Insert
End If
Next
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
End Sub
Unsorted array :
Classeur2.xlsm | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | |||
1 | Jeune | Père | Mère | Eleveur | Age | Volière | Cage | Né(e) | Tours | Information | Elevage | N° Ligne | ||
2 | AEY27 093/2012 M | AET27-093/2022 M | AE27-093/2012 M | Gérard Claude | 10a 11m 28j | 2B | 16 | 8-06-2012 | 4T | 1 | ||||
3 | AE27-094/2012 M | AE27-093/2012 M | AE27-094/2012 M | Gérard Claude | 10a 11m 28j | 2B | 16 | 8-06-2012 | 4T | x | 2 | |||
4 | HTY0127 100/2012 F | AE27-093/2012 M | AE27-100/2012 F | Gérard Claude | 10a 11m 26j | 2B | 13 | 10-06-2012 | 4T | 3 | ||||
5 | AE27-059/2013 F | AE27-059/2013 F | AE27-059/2013 F | Gérard Claude | 10a 1m 11j | 3H | 13 | 25-04-2013 | 4T | x | 4 | |||
6 | AE27060/2013 M | AE27-059/2013 F | AE27-060/2013 M | Gérard Claude | 10a 0m 31j | 5H | 19 | 5-05-2013 | 5T | 5 | ||||
7 | AE27-084/2013 M | AE27-084/2013 M | AE27-084/2013 M | Gérard Claude | 10a 0m 17j | 3H | 16 | 19-05-2013 | 4T | 6 | ||||
8 | AE27085/2013 M | AE27-084/2013 M | AE27-085/2013 M | Gérard Claude | 10a 0m 18j | 5H | 21 | 18-05-2013 | 4T | x | 7 | |||
9 | AE27 086/2013 F | AE27-084/2013 M | AE27-086/2013 F | Gérard Claude | 9a 11m 9j | 2B | 21 | 27-06-2013 | 4T | 8 | ||||
10 | AE27-087/2013 F | AE27-087/2013 F | AE27-087/2013 F | Gérard Claude | 10a 0m 18j | 5H | 22 | 18-05-2013 | 4T | x | 9 | |||
11 | AE27011/2019 F | AE27-087/2013 F | AE27-011/2019 F | Gérard Claude | 4a 1m 14j | 4H | 11 | 22-04-2019 | 5T | x | 10 | |||
12 | AE27012/2019 F | AE27-087/2013 F | AE27-012/2019 F | Gérard Claude | 4a 1m 14j | 5H | 11 | 22-04-2019 | 5T | x | 11 | |||
13 | AE27013/2019 M | AE27-013/2019 M | AE27-013/2019 M | Gérard Claude | 4a 1m 14j | 4H | 1 | 22-04-2019 | 4T | x | 12 | |||
14 | AE27014/2019 F | AE27-014/2019 F | AE27-014/2019 F | Gérard Claude | 4a 1m 16j | 4H | 2 | 20-04-2019 | 4T | 13 | ||||
15 | AE27015/2019 M | AE27-014/2019 F | AE27-015/2019 M | Gérard Claude | 4a 1m 17j | 4H | 6 | 19-04-2019 | 4T | x | 14 | |||
16 | AE27 017/2019 F | AE27-017/2019 F | AE27-017/2019 F | Gérard Claude | 4a 1m 17j | 3H | 6 | 19-04-2019 | 4T | 15 | ||||
17 | AE27-042/2022 M | AE27-042/2022 M | AE27-042/2022 M | Gérard Claude | 0a 11m 18j | 5H | 6 | 18-06-2022 | 4T | x | 16 | |||
18 | AE27 043/2022 F | AE27-042/2022 M | AE27-043/2022 F | Gérard Claude | 0a 11m 18j | 4H | 6 | 18-06-2022 | 4T | x | 17 | |||
Résultat |
Unless I am mistaken, here is the table sorted on column "B" with rows inserted :
Classeur2.xlsm | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | |||
1 | Jeune | Père | Mère | Eleveur | Age | Volière | Cage | Né(e) | Tours | Information | Elevage | N° Ligne | ||
2 | AE27013/2019 M | AE27-013/2019 M | AE27-013/2019 M | Gérard Claude | 4a 1m 14j | 4H | 1 | 22-04-2019 | 4T | x | 12 | |||
3 | ||||||||||||||
4 | AE27014/2019 F | AE27-014/2019 F | AE27-014/2019 F | Gérard Claude | 4a 1m 16j | 4H | 2 | 20-04-2019 | 4T | 13 | ||||
5 | AE27015/2019 M | AE27-014/2019 F | AE27-015/2019 M | Gérard Claude | 4a 1m 17j | 4H | 6 | 19-04-2019 | 4T | x | 14 | |||
6 | ||||||||||||||
7 | AE27 017/2019 F | AE27-017/2019 F | AE27-017/2019 F | Gérard Claude | 4a 1m 17j | 3H | 6 | 19-04-2019 | 4T | 15 | ||||
8 | ||||||||||||||
9 | AE27-042/2022 M | AE27-042/2022 M | AE27-042/2022 M | Gérard Claude | 0a 11m 18j | 5H | 6 | 18-06-2022 | 4T | x | 16 | |||
10 | AE27 043/2022 F | AE27-042/2022 M | AE27-043/2022 F | Gérard Claude | 0a 11m 18j | 4H | 6 | 18-06-2022 | 4T | x | 17 | |||
11 | ||||||||||||||
12 | AE27-059/2013 F | AE27-059/2013 F | AE27-059/2013 F | Gérard Claude | 10a 1m 11j | 3H | 13 | 25-04-2013 | 4T | x | 4 | |||
13 | AE27060/2013 M | AE27-059/2013 F | AE27-060/2013 M | Gérard Claude | 10a 0m 31j | 5H | 19 | 5-05-2013 | 5T | 5 | ||||
14 | ||||||||||||||
15 | AE27-084/2013 M | AE27-084/2013 M | AE27-084/2013 M | Gérard Claude | 10a 0m 17j | 3H | 16 | 19-05-2013 | 4T | 6 | ||||
16 | AE27085/2013 M | AE27-084/2013 M | AE27-085/2013 M | Gérard Claude | 10a 0m 18j | 5H | 21 | 18-05-2013 | 4T | x | 7 | |||
17 | AE27 086/2013 F | AE27-084/2013 M | AE27-086/2013 F | Gérard Claude | 9a 11m 9j | 2B | 21 | 27-06-2013 | 4T | 8 | ||||
18 | ||||||||||||||
19 | AE27-087/2013 F | AE27-087/2013 F | AE27-087/2013 F | Gérard Claude | 10a 0m 18j | 5H | 22 | 18-05-2013 | 4T | x | 9 | |||
20 | AE27011/2019 F | AE27-087/2013 F | AE27-011/2019 F | Gérard Claude | 4a 1m 14j | 4H | 11 | 22-04-2019 | 5T | x | 10 | |||
21 | AE27012/2019 F | AE27-087/2013 F | AE27-012/2019 F | Gérard Claude | 4a 1m 14j | 5H | 11 | 22-04-2019 | 5T | x | 11 | |||
22 | ||||||||||||||
23 | AE27-094/2012 M | AE27-093/2012 M | AE27-094/2012 M | Gérard Claude | 10a 11m 28j | 2B | 16 | 8-06-2012 | 4T | x | 2 | |||
24 | HTY0127 100/2012 F | AE27-093/2012 M | AE27-100/2012 F | Gérard Claude | 10a 11m 26j | 2B | 13 | 10-06-2012 | 4T | 3 | ||||
25 | ||||||||||||||
26 | AEY27 093/2012 M | AET27-093/2022 M | AE27-093/2012 M | Gérard Claude | 10a 11m 28j | 2B | 16 | 8-06-2012 | 4T | 1 | ||||
Résultat |