1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| Option Explicit
Public Sub TransformeListeCasino()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim commune As String
Dim casino As String
Dim adresse As String
Dim codePostal As String
Dim ville As String
Dim complementAdresse As String
Dim societe As String
Dim i1 As Integer, i2 As Integer
Dim j1 As Integer, j2 As Integer
Dim maxRow As Integer
Dim row2 As Integer
Set ws1 = Worksheets("Feuil1")
Set ws2 = Worksheets("Feuil2")
maxRow = ws1.Range("B65000").End(xlUp).Row
ws2.Range("A2:G65000").ClearContents
i1 = 2
row2 = 2
commune = ws1.Cells(i1, 1).Value
Do While commune <> ""
'On cherche la ligne de la commune suivante
i2 = i1 + 1
Do While ws1.Cells(i2, 1).Value = "" And i2 <= maxRow
i2 = i2 + 1
Loop
'On cherche la première ligne non vide en colonne B
j1 = i1
Do While ws1.Cells(j1, 2).Value = ""
j1 = j1 + 1
Loop
'On cherche la dernière ligne non vide en colonne B
j2 = i2 - 1
Do While ws1.Cells(j2, 2).Value = ""
j2 = j2 - 1
Loop
societe = ws1.Cells(i1, 3).Value
casino = ws1.Cells(j1, 2).Value
adresse = ws1.Cells(j2 - 1, 2).Value
codePostal = ws1.Cells(j2, 2).Value
If Len(codePostal) > 6 Then
ville = Right(codePostal, Len(codePostal) - 6)
Else
ville = ""
End If
codePostal = Left(codePostal, 5)
complementAdresse = IIf(j2 - j1 >= 3, ws1.Cells(j2 - 2, 2).Value, "")
ws2.Cells(row2, 1).Value = commune
ws2.Cells(row2, 2).Value = casino
ws2.Cells(row2, 3).Value = adresse
ws2.Cells(row2, 4).Value = codePostal
ws2.Cells(row2, 5).Value = ville
ws2.Cells(row2, 6).Value = complementAdresse
ws2.Cells(row2, 7).Value = societe
row2 = row2 + 1
i1 = i2
commune = ws1.Cells(i1, 1).Value
Loop
End Sub |