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

Boucle pour nommer des cellules sur une sélection

3 réponses
Avatar
Emile63
Bonjour =C3=A0 tous,

Je cherche a nommer des cellules avec une boucle (qui change le Nom a chaqu=
e fois) a partir d'une s=C3=A9lection, je brode autour de ce code, mais il =
y a un truc qui m'=C3=A9chappe...
Merci d'avance pour votre aide.
Emile
----------------------------------------------------------------
Sub InsertionNoms()
'G=C3=A9n=C3=A9rer des noms en boucle avec un format pr=C3=A9d=C3=A9termin=
=C3=A9
Dim Cel As Range, Incr=C3=A9ment As String
On Error Resume Next
For Each Cel In Selection
If Cel.Value <> "" Then
Incr=C3=A9ment =3D Format(Incr=C3=A9ment + 1, "000")
Nom =3D "_01_Trad_" & Incr=C3=A9ment
Cel.Names.Add Name:=3DNom
End If
Next
End Sub
----------------------------------------------------------------

3 réponses

Avatar
MichD
Le 01/09/20 à 04:46, Emile63 a écrit :
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As String
On Error Resume Next
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Format(Incrément + 1, "000")
Nom = "_01_Trad_" & Incrément
Cel.Names.Add Name:=Nom
End If
Next
End Sub

Bonjour,
Comme ceci :
'--------------------------------
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As Long, Nom As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Incrément = Format(Incrément, "000")
Nom = "_01_Trad_" & Incrément
Cel.Name = Nom
End If
Next
End If
End Sub
'--------------------------------
MichD
Avatar
MichD
Le 01/09/20 à 06:38, Emile63 a écrit :
Le Tuesday, September 1, 2020 à 12:10:31 PM UTC+2, MichD a écrit :
Le 01/09/20 à 04:46, Emile63 a écrit :
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As String
On Error Resume Next
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Format(Incrément + 1, "000")
Nom = "_01_Trad_" & Incrément
Cel.Names.Add Name:=Nom
End If
Next
End Sub

Bonjour,
Comme ceci :
'--------------------------------
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As Long, Nom As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Incrément = Format(Incrément, "000")
Nom = "_01_Trad_" & Incrément
Cel.Name = Nom
End If
Next
End If
End Sub
'--------------------------------
MichD

Bonjour MichD et merci pour ton aide.
Le formatage "000" ne prend pas. :(
-Sans doutes dû à la déclaration "Long" j'ai donc ajouté une étape supplémentaire "Position" (String).
J'avoue que je n'ai pas compris à quoi sert le "If TypeName(Selection) = "Range" mais comme ça fonctionne... :)
Je te remercie pour ton aide.
Et très bonne journée.
Emile
------------------------------------
Sub InsertionNoms2()
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As Long, Nom As String, Position As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Position = Format(Incrément, "000")
Nom = "_01_Trad_" & Position
Cel.Name = Nom
End If
Next
End If
End Sub
------------------------------------

Désolé, ce détail m'a échappé...
comme ceci :
------------------------------------
Sub InsertionNoms()
Dim X As String
'Générer des noms en boucle avec un format prédéterminé
Dim Cel As Range, Incrément As Long, Nom As String, Nb As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Nb = Format(CStr(Incrément), "000")
Nom = "_01_Trad_" & Nb
Cel.Name = Nom
End If
Next
End If
End Sub
------------------------------------
MichD
Avatar
Emile63
Le Tuesday, September 1, 2020 à 12:53:43 PM UTC+2, MichD a écrit  :
Le 01/09/20 à 06:38, Emile63 a écrit :
Le Tuesday, September 1, 2020 à 12:10:31 PM UTC+2, MichD a éc rit :
Le 01/09/20 à 04:46, Emile63 a écrit :
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédét erminé
Dim Cel As Range, Incrément As String
On Error Resume Next
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Format(Incrément + 1, "000")
Nom = "_01_Trad_" & Incrément
Cel.Names.Add Name:=Nom
End If
Next
End Sub
Bonjour,
Comme ceci :
'--------------------------------
Sub InsertionNoms()
'Générer des noms en boucle avec un format prédéte rminé
Dim Cel As Range, Incrément As Long, Nom As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Incrément = Format(Incrément, "000")
Nom = "_01_Trad_" & Incrément
Cel.Name = Nom
End If
Next
End If
End Sub
'--------------------------------
MichD

Bonjour MichD et merci pour ton aide.
Le formatage "000" ne prend pas. :(
-Sans doutes dû à la déclaration "Long" j'ai donc ajout é une étape supplémentaire "Position" (String).
J'avoue que je n'ai pas compris à quoi sert le "If TypeName(Select ion) = "Range" mais comme ça fonctionne... :)
Je te remercie pour ton aide.
Et très bonne journée.
Emile
------------------------------------
Sub InsertionNoms2()
'Générer des noms en boucle avec un format prédéter miné
Dim Cel As Range, Incrément As Long, Nom As String, Position As St ring
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Position = Format(Incrément, "000")
Nom = "_01_Trad_" & Position
Cel.Name = Nom
End If
Next
End If
End Sub
------------------------------------
Désolé, ce détail m'a échappé...
comme ceci :
------------------------------------
Sub InsertionNoms()
Dim X As String
'Générer des noms en boucle avec un format prédétermi né
Dim Cel As Range, Incrément As Long, Nom As String, Nb As String
If TypeName(Selection) = "Range" Then
For Each Cel In Selection
If Cel.Value <> "" Then
Incrément = Incrément + 1
Nb = Format(CStr(Incrément), "000")
Nom = "_01_Trad_" & Nb
Cel.Name = Nom
End If
Next
End If
End Sub
------------------------------------
MichD

Ok, merci et bonne fn de journée.
Emile