Exécuter une macro sur différents onglets

13mars93 -  
 13mars93 -
Bonsoir à tous,

Je suppose que pour vous cela doit être simple pour vous, mais pour moi c'est compliqué.

Je viens de créer une Macro (c'est une première).
La macro s'exécute bien sur l'onglet actif.

J'aimerai que la Macro s'exécute sur d'autres onglets du même fichier.
La macro consiste à de la mise en page : "supprimer des espaces" et "supprimer <<"

voici ma macro :

Sub Supprimer_Espaces()
Cells.Find(What:="Cout global", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "Cout global"
Range("B79").Select
Cells.Find(What:="Heures travaillées", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.FormulaR1C1 = "Heures Travaillées"
Range("B80").Select
End Sub

Merci d'avance pour vos précieuses réponses.

A voir également:

6 réponses

Patrice33740 Messages postés 8930 Statut Membre 1 781
 
Essaies ce code :
Option Explicit
Sub Supprimer_Espaces()
Dim wsh As Worksheet
Dim cel As Range

For Each wsh In ActiveWorkbook.Worksheets
  Set cel = Cells.Find(What:="Cout global", After:=ActiveCell, _
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
  If Not cel Is Nothing Then cel.Formula = "Cout global"
  Set cel = Cells.Find(What:="Heures travaillées", After:=ActiveCell, _
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
  If Not cel Is Nothing Then cel.Formula = "Heures Travaillées"
Next wsh

End Sub
0
13mars93
 
Merci pour votre réactivité et votre rapide réponse.

Malheureusement, cela fonctionne que sur l'onglet actif.
La macro ne s'exécute pas sur les autres onglets.

à l'aide de vos éléments, voilà ma nouvelle macro :

Sub Supprimer_Espaces()
Dim wsh As Worksheet
Dim cel As Range

For Each wsh In ActiveWorkbook.Worksheets
Set cel = Cells.Find(What:="Cout global", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cel Is Nothing Then cel.Formula = "Cout global"
Set cel = Cells.Find(What:="Heures travaillées", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
If Not cel Is Nothing Then cel.Formula = "Heures Travaillées"
Next wsh

End Sub


Avez-vous une autre idée ?
merci
0
Patrice33740 Messages postés 8930 Statut Membre 1 781
 
Re,

Excuse-moi, j'ai été un peu trop rapide, voici le code :
Option Explicit
Sub Supprimer_Espaces()
Dim wsh As Worksheet
Dim cel As Range

For Each wsh In ActiveWorkbook.Worksheets
  Set cel = wsh.Cells.Find(What:="Cout global", After:=ActiveCell, _
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
  If Not cel Is Nothing Then cel.Formula = "Cout global"
  Set cel = wsh.Cells.Find(What:="Heures travaillées", After:=ActiveCell, _
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
  If Not cel Is Nothing Then cel.Formula = "Heures Travaillées"
Next wsh

End Sub

0
13mars93
 
Parfait ! Merci pour votre aide.
c'est marche parfaitement.
je dormirai moins bête ce soir grâce à vous.

Je peux vous soumettre une autre hypothèse ?

Dans le cas où, j'aurai plusieurs onglets avant et après et que ces dernières ne doivent pas être affectées par la macro.
Puis-je spécifier les onglets sur lesquels la macro doit être exécuter ?

exple :
- les onglets compris entre la feuille AAAA et la feuille ZZZZ doivent être concernés par la macro.
ou
- les onglets n°1 ; 7 ; 23 ; ... ; 99 sont concernés par la macro

Merci d'avance.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Patrice33740 Messages postés 8930 Statut Membre 1 781
 
Re

1er cas :
Sub Supprimer_Espaces() 
Dim wsh As Worksheet 
Dim cel As Range 

For Each wsh In ActiveWorkbook.Worksheets 
Select Case wsh.Name 
Case "AAAA", "ZZZZ" 
  Set cel = wsh.Cells.Find(What:="Cout global", After:=ActiveCell, _ 
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
  If Not cel Is Nothing Then cel.Formula = "Cout global" 
  Set cel = wsh.Cells.Find(What:="Heures travaillées", After:=ActiveCell, _ 
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
  If Not cel Is Nothing Then cel.Formula = "Heures Travaillées" 
End Select 
Next wsh 
End Sub


2ème cas
Sub Supprimer_Espaces() 
Dim wsh As Worksheet 
Dim cel As Range 

For Each wsh In ActiveWorkbook.Worksheets 
Select Case wsh.Index 
Case 1, 7, 23 To 99 
  Set cel = wsh.Cells.Find(What:="Cout global", After:=ActiveCell, _ 
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
  If Not cel Is Nothing Then cel.Formula = "Cout global" 
  Set cel = wsh.Cells.Find(What:="Heures travaillées", After:=ActiveCell, _ 
       LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
       SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
  If Not cel Is Nothing Then cel.Formula = "Heures Travaillées" 
End Select 
Next wsh 
End Sub


Code à adapter en fonction du nom ou du numéro de feuille
Cordialement
Patrice
0
13mars93
 
Un Grand MERCI à vous Patrice d'avoir pris le temps pour résoudre ma problématique.

Ce n'est pas tout à fait ce que je demandais, mais je m'en contente pleinement pour le moment où je n'ai pas beaucoup d'onglets.
Donc, j'ai nommé dans ma macro les onglets concernés.

Vous êtes vraiment doué. Passez une bonne soirée.
Encore merci
Cdt,
0