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
| Sub TrouverNomColonneNonVide()
Dim ws As Worksheet
Dim lastRow As Long
Dim lastCol As Long
Dim i As Long
Dim j As Long
Dim colonneTrouvee As String
' Spécifier la feuille de calcul
Set ws = ThisWorkbook.Sheets("Feuil1")
' Trouver la dernière ligne en se basant sur la première colonne du tableau
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Trouver la dernière colonne
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' Parcourir chaque ligne de haut en bas
For i = 2 To lastRow
' Parcourir chaque colonne de la dernière colonne-1 vers la gauche
For j = lastCol - 1 To 1 Step -1
If Not IsEmpty(ws.Cells(i, j)) Then
' Cellule non vide trouvée, enregistrer le nom de la colonne
colonneTrouvee = ws.Cells(1, j).Value
ws.Cells(i, lastCol).Value = colonneTrouvee
Exit For
End If
Next j
Next i
End Sub |