Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Automatisation macro en boucle?

1 réponse
Avatar
Albert
Bonsoir,

Je voudrais transformer cette macro en boucle (car j'ai environ 100 lignes à
traiter) mais je bloque! ;(

If Sheets("feui1").Range("B1") <> "1" Then
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("B1").Select
ActiveCell.FormulaR1C1 = "1"
Else
End If

If Sheets("feui1").Range("B2") <> "2" Then
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Range("B2").Select
ActiveCell.FormulaR1C1 = "2"
Else
End If

...etc pour 3,4,... et 100.


=> boucle :

Sub ajout_ligne_colB()
' Trier le tableau par la colonne B avant

For i = [A65536].End(xlUp).Row To 1 Step -1
If Range("B" & i).Value <> i Then
Rows(i).Select
Selection.Insert Shift:=xlDown
Range("B" & i).Select
ActiveCell.FormulaR1C1 = i
End If
Next i

End Sub




100 mercis d'avance ;)
Al

1 réponse

Avatar
FxM
Bonsoir,

A part les points suivants, ca devrait aller.
- [A65536] me paraîtrait devoir être [B65536]
- sheets("feuil1") n'est pas repris.
(détail : il n'est pas forcé de sélectionner pour faire)

Sub ajout_ligne_colB()
' Trier le tableau par la colonne B avant
For i = [B65536].End(xlUp).Row To 1 Step -1
If Range("B" & i).Value <> i Then

'si insérer une ligne complète
' Rows(i).entirerow.insert Shift:=xlDown

'si insérer sur une cellule
' Rows(i).insert Shift:=xlDown

Range("B" & i).value = i
End If
Next i
End Sub


@+
FxM



Bonsoir,

Je voudrais transformer cette macro en boucle (car j'ai environ 100
lignes à traiter) mais je bloque! ;(

If Sheets("feui1").Range("B1") <> "1" Then
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("B1").Select
ActiveCell.FormulaR1C1 = "1"
Else
End If

If Sheets("feui1").Range("B2") <> "2" Then
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Range("B2").Select
ActiveCell.FormulaR1C1 = "2"
Else
End If

....etc pour 3,4,... et 100.


=> boucle :

Sub ajout_ligne_colB()
' Trier le tableau par la colonne B avant

For i = [A65536].End(xlUp).Row To 1 Step -1
If Range("B" & i).Value <> i Then
Rows(i).Select
Selection.Insert Shift:=xlDown
Range("B" & i).Select
ActiveCell.FormulaR1C1 = i
End If
Next i

End Sub




100 mercis d'avance ;)
Al