Problème de boucle avec for

Résolu
Bonjour,
Je n'arrive pas a faire une macro exel qui me conte le nombre de cellule non vide dans une colone.

Je fait la boucle suivante mais ca ne marche pas

Dim i As Integer
Dim var As Variant

i = 1
var = 1

For i = 1 To var
If Cells(i, 1) <> "" Then
var = i + 1

Else
Exit For

End If
Next i

2 réponses

  • Votes
  • Date
  • Date décroissante
  1. Bonjour,

    tu peux essayer ceci :

    Dim iLineRead As Integer
    Dim iNbLine As Integer
    Dim iNbCellNoEmpty As Integer
    
    iLineRead = 1
    iNbLine = Sheets(1).Range("A65536").End(xlUp).Row 
    iNbCellNoEmpty = 0
    
    
    Do Until iLineRead = iNbLine
        If Trim(Sheets(1).Cells(iLineRead, 1)) <> "" Then
            iNbCellNoEmpty = iNbCellNoEmpty + 1
        End If
    
        iLineRead = iLineRead + 1
    Loop
    
    MsgBox "Nombre de cellule non vide = " & iNbCellNoEmpty
    
    1. Avec un For cela donnera ceci :

      Dim iLineRead As Integer
      Dim iNbLine As Integer
      Dim iNbCellNoEmpty As Integer
      
      iLineRead = 1
      iNbLine = Sheets(1).Range("A65536").End(xlUp).Row 
      iNbCellNoEmpty = 0
      
      For iLineRead = 1 To iNbLine
          If Trim(Sheets(1).Cells(iLineRead, 1)) <> "" Then
              iNbCellNoEmpty = iNbCellNoEmpty + 1
          End If
      Next iLineRead
      
      MsgBox "Nombre de cellule non vide = " & iNbCellNoEmpty