problème sur une boucle for

sebounechouchou

XLDnaute Nouveau
bonjour à tous
j'ai un classeur excel dans lequel j'ai plusieurs feuilles, j'ai crée une boucle pour copier ds données sur chacune des feuilles mais la recopie ne se fait que sur une seule feuille!!
Pouvez vous m'aider
voici la syntaxe :

Private Sub CommandButton1_Click()
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
a = TextBox1
Sheets("tableau maître").Activate
ActiveSheet.Unprotect
b = Cells(12, 12)
i = 12
j = 12
Do While Cells(i, j) <> ""
j = j + 4
Loop
Cells(i, j) = a
Range("l13", "o44").Select
Selection.Copy Destination:=Cells(13, j)
Range(Cells(14, j), Cells(44, j + 4)).Select
Selection.ClearContents
Next WS
TextBox1.Value = ""
UserForm1.Hide
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("tableau maître").Select
End Sub
 

sebounechouchou

XLDnaute Nouveau
Re : problème sur une boucle for

coucou toujours le même code j'ai une erreur sur la passage en route, erreur 1004
Private Sub CommandButton1_Click()
Sheets("tableau maître").Unprotect
Dim Ws As Worksheet

For Each Ws In ThisWorkbook.Worksheets
With Ws
a = TextBox1
b = .Cells(12, 13)
i = 12
j = 13
Do While .Cells(i, j) <> ""
j = j + 4
Loop
.Cells(i, j) = a
.Range(.Cells(i, j), .Cells(i, j + 3)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
.Range("m13", "p48").Copy Destination:=.Cells(13, j)
.Range(.Cells(14, j), .Cells(44, j + 3)).ClearContents
End With
Next Ws

TextBox1.Value = ""
UserForm1.Hide
Sheets("tableau maître").Protect
End Sub
 

sebounechouchou

XLDnaute Nouveau
Re : problème sur une boucle for

je sais bien mais je pensais qu'avec ma boucle for each ws j'activais donc la page et puis cela marche avec cette syntaxe là :

Private Sub CommandButton1_Click()
Sheets("tableau maître").Unprotect
Dim Ws As Worksheet
For Each Ws In ThisWorkbook.Worksheets
With Ws
a = TextBox1
b = .Cells(12, 12)
i = 12
j = 12
Do While .Cells(i, j) <> ""
j = j + 4
Loop
.Cells(i, j) = a
.Range(.Cells(i, j), .Cells(i, j + 3)).Merge
.Range("l13", "o48").Copy Destination:=.Cells(13, j)
.Range(.Cells(14, j), .Cells(44, j + 3)).ClearContents
End With
Next Ws
TextBox1.Value = ""
UserForm1.Hide
Sheets("tableau maître").Protect
End Sub

c'est juste que là je voulais rajouter une ligne pour mettre à la ligne de façon automatique si le texte est trop long
 

sebounechouchou

XLDnaute Nouveau
Re : problème sur une boucle for

bonjour je reveins vers vous car je ne trrouve toujours pas de solution à mon problème
pierrot93 m'a bien aidé sur ma boucle for each mais maintenan je veux dans cette boucle faire une mise en forme sur les cellules d'où le code suivant :

For Each Ws In ThisWorkbook.Worksheets
With Ws
a = TextBox1
b = .Cells(12, 13)
i = 12
j = 13
Do While .Cells(i, j) <> ""
j = j + 4
Loop
.Cells(i, j) = a
.Range(.Cells(i, j), .Cells(i, j + 3)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
.Range("m13", "p48").Copy Destination:=.Cells(13, j)
.Range(.Cells(14, j), .Cells(44, j + 3)).ClearContents
End With
Next Ws

TextBox1.Value = ""
UserForm1.Hide
Sheets("tableau maître").Protect
End Sub

mais j'ai une erreur sur le la ligne en rouge erreur 1004 la selection de la méthode range a échoué, merci de votre aide
 

Pierrot93

XLDnaute Barbatruc
Re : problème sur une boucle for

Re

C'est la méthode "select" qui ne peut s'appliquer que sur un objet "range" de la feuille active... Par contre tu peux appliquer à ce "range", les propriétés comme "merge" sans que la feuille soit active....
 

Discussions similaires

Réponses
0
Affichages
154

Statistiques des forums

Discussions
312 273
Messages
2 086 695
Membres
103 372
dernier inscrit
BibiCh