Advertisement
bipping

Excel sior

Nov 2nd, 2023
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 1.09 KB | Source Code | 0 0
  1. Sub NumeroterColonneA()
  2.     Dim LastRow As Long
  3.     Dim i As Long
  4.     Dim Count As Long
  5.     Dim ws As Worksheet
  6.  
  7.     Set ws = ThisWorkbook.Sheets("Feuil1")
  8.    
  9.     ' Trouver la dernière ligne avec des données dans la colonne D
  10.    LastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
  11.  
  12.     Count = 1 ' Initialisation du compteur à 1
  13.  
  14.     For i = 3 To LastRow
  15.         ' Si la cellule en colonne D n'est pas vide, alors...
  16.        If Not IsEmpty(ws.Cells(i, 4)) Then
  17.             ' Attribuer le numéro dans la colonne A
  18.            ws.Cells(i, 1).Value = Count
  19.             Count = Count + 1
  20.         Else
  21.             ' Si la cellule en colonne D est vide, laisser la cellule en colonne A vide
  22.            ws.Cells(i, 1).Value = ""
  23.         End If
  24.     Next i
  25.  
  26.     ' Parcourir à nouveau pour s'assurer que les cellules vides en colonne A prennent la valeur de la dernière cellule non vide
  27.    For i = 4 To LastRow
  28.         If ws.Cells(i, 1).Value = "" And ws.Cells(i - 1, 1).Value <> "" Then
  29.             ws.Cells(i, 1).Value = ws.Cells(i - 1, 1).Value + 1
  30.         End If
  31.     Next i
  32. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement