Je veux que les ligne ressort soit freeze pour ses affectation
Sub DeployerAffectations()
Dim wsIn As Worksheet
Dim wsOut As Worksheet
Dim lastIn As Long
Dim i As Long, j As Long, k As Long
Dim ligneOut As Long
Dim cle As Variant
Dim collRessorts As New Collection
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
'---- 1) RéFéRENCEMENT DES FEUILLES ----
' Définir la feuille source
Set wsIn = ThisWorkbook.Worksheets("Feuil1")
' Créer/initialiser la feuille de destination
On Error Resume Next
Set wsOut = ThisWorkbook.Worksheets("FeuilResultat")
On Error GoTo 0
If wsOut Is Nothing Then
Set wsOut = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
wsOut.Name = "FeuilResultat"
Else
wsOut.Cells.Clear
End If
'---- 2) VIDE LA FEUILLE DE SORTIE ----
wsOut.Cells.Clear
'---- 3) CREATION DE L'EN-TETE ----
With wsOut
.Range("A1:C1").Value = Array("POSTES OFFERTS", "NOMS - PRÉNOMS", "SIGNATURES")
With .Range("A1:C1")
.Font.Bold = True
.Font.Size = 24
.Interior.Color = RGB(200, 200, 200)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
'---- 4) FIGER LA LIGNE 1 ----
wsOut.Activate
wsOut.Range("A2").Select
ActiveWindow.FreezePanes = True
'---- 3) DéTECTION DERNIéRE LIGNE DE LA SOURCE ----
lastIn = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
If lastIn < 2 Then
MsgBox "Pas de données en Feuil1.", vbExclamation
Exit Sub
End If
'---- 4) RéCUPéRER LA LISTE DES RESSORTS DANS L'ORDRE D'APPARITION ----
On Error Resume Next
For i = 2 To lastIn
cle = CStr(wsIn.Cells(i, "A").Value)
If Len(cle) > 0 Then
collRessorts.Add cle, cle
End If
Next i
On Error GoTo 0
'---- 5) BOUCLE DE GéNéRATION ----
ligneOut = 2
For Each cle In collRessorts
' 5.1) écris le titre du Ressort
With wsOut.Range(wsOut.Cells(ligneOut, "A"), wsOut.Cells(ligneOut, "C"))
.Merge ' fusionne A et C sur la méme ligne
.Value = cle ' met le titre
.Font.Bold = True ' en gras
.Font.Size = 24 ' en taille 24
.HorizontalAlignment = xlCenter ' centre le texte en H
.VerticalAlignment = xlCenter ' centre le texte en V
.Interior.Color = RGB(173, 216, 230) ' définir la color
.Rows.RowHeight = 35.14 ' définir la hauteur de la ligne
End With
ligneOut = ligneOut + 1
' 5.2) Pour chaque ligne source qui a ce Ressort, déploie l'affectation
For i = 2 To lastIn
If wsIn.Cells(i, "A").Value = cle Then
Dim nbPostes As Long
nbPostes = CLng(wsIn.Cells(i, "C").Value)
Dim aff As String
aff = wsIn.Cells(i, "B").Value
For k = 1 To nbPostes
With wsOut.Cells(ligneOut, "A")
.Value = aff
.Font.Size = 18
.Rows.RowHeight = 35.14
End With
ligneOut = ligneOut + 1
Next k
End If
Next i
Next cle
'---- 6) OPTIONNEL : mise en largeur automatique ----
' wsOut.Columns("A").AutoFit
wsOut.Columns("A:C").ColumnWidth = 74
' MsgBox "Tableau généré avec succés dans " & wsOut.Name & "!", vbInformation
End Sub
Pour que les lignes de résultat soient figées pour leurs affectations, vous devez vous assurer que la ligne d'en-tête reste visible lorsque vous faites défiler la feuille de calcul. Vous avez déjà utilisé ActiveWindow.FreezePanes = True pour figer la première ligne, mais il est important de s'assurer que cette ligne est toujours visible.
Voici quelques ajustements pour garantir que la ligne d'en-tête reste figée et que le script fonctionne correctement :
Vérifier que la ligne d'en-tête est correctement figée: La ligne ActiveWindow.FreezePanes = True doit être placée après la création de l'en-tête et avant de commencer à écrire les données.
Éviter les redondances: Vous avez deux sections pour vider la feuille de sortie. Une seule suffit.
Optimiser le code: Assurez-vous que les opérations de mise en forme et de manipulation des données sont optimisées pour éviter les erreurs.
Voici le script ajusté :
vba
Sub DeployerAffectations()
Dim wsIn As Worksheet
Dim wsOut As Worksheet
Dim lastIn As Long
Dim i As Long, j As Long, k As Long
Dim ligneOut As Long
Dim cle As Variant
Dim collRessorts As New Collection
'---- 1) RéFéRENCEMENT DES FEUILLES ----
' Définir la feuille source
Set wsIn = ThisWorkbook.Worksheets("Feuil1")
' Créer/initialiser la feuille de destination
On Error Resume Next
Set wsOut = ThisWorkbook.Worksheets("FeuilResultat")
On Error GoTo 0
If wsOut Is Nothing Then
Set wsOut = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
wsOut.Name = "FeuilResultat"
Else
wsOut.Cells.Clear
End If
'---- 2) VIDE LA FEUILLE DE SORTIE ----
wsOut.Cells.Clear
'---- 3) CREATION DE L'EN-TETE ----
With wsOut
.Range("A1:C1").Value = Array("POSTES OFFERTS", "NOMS - PRÉNOMS", "SIGNATURES")
With .Range("A1:C1")
.Font.Bold = True
.Font.Size = 24
.Interior.Color = RGB(200, 200, 200)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
'---- 4) FIGER LA LIGNE 1 ----
wsOut.Activate
wsOut.Range("A2").Select
ActiveWindow.FreezePanes = True
'---- 5) DÉTECTION DERNIÈRE LIGNE DE LA SOURCE ----
lastIn = wsIn.Cells(wsIn.Rows.Count, "A").End(xlUp).Row
If lastIn < 2 Then
MsgBox "Pas de données en Feuil1.", vbExclamation
Exit Sub
End If
'---- 6) RÉCUPÉRER LA LISTE DES RESSORTS DANS L'ORDRE D'APPARITION ----
On Error Resume Next
For i = 2 To lastIn
cle = CStr(wsIn.Cells(i, "A").Value)
If Len(cle) > 0 Then
collRessorts.Add cle, cle
End If
Next i
On Error GoTo 0
'---- 7) BOUCLE DE GÉNÉRATION ----
ligneOut = 2
For Each cle In collRessorts
' 7.1) Écris le titre du Ressort
With wsOut.Range(wsOut.Cells(ligneOut, "A"), wsOut.Cells(ligneOut, "C"))
.Merge ' fusionne A et C sur la même ligne
.Value = cle ' met le titre
.Font.Bold = True ' en gras
.Font.Size = 24 ' en taille 24
.HorizontalAlignment = xlCenter ' centre le texte en H
.VerticalAlignment = xlCenter ' centre le texte en V
.Interior.Color = RGB(173, 216, 230) ' définir la couleur
.Rows.RowHeight = 35.14 ' définir la hauteur de la ligne
End With
ligneOut = ligneOut + 1
' 7.2) Pour chaque ligne source qui a ce Ressort, déploie l'affectation
For i = 2 To lastIn
If wsIn.Cells(i, "A").Value = cle Then
Dim nbPostes As Long
nbPostes = CLng(wsIn.Cells(i, "C").Value)
Dim aff As String
aff = wsIn.Cells(i, "B").Value
For k = 1 To nbPostes
With wsOut.Cells(ligneOut, "A")
.Value = aff
.Font.Size = 18
.Rows.RowHeight = 35.14
End With
ligneOut = ligneOut + 1
Next k
End If
Next i
Next cle
'---- 8) OPTIONNEL : mise en largeur automatique ----
' wsOut.Columns("A").AutoFit
wsOut.Columns("A:C").ColumnWidth = 74
' MsgBox "Tableau généré avec succès dans " & wsOut.Name & "!", vbInformation
End Sub
Ce script garantit que la ligne d'en-tête reste figée et que les données sont correctement déployées dans la feuille de sortie.