Traduire en VBA
Bonsoir
je me lance dans un petit exercice de consistant à traduire le texte si dessous en vba , si vous pouviez m'apporter une correction
en vous remerciant par avance , car je pense faire pas mal d'erreur de syntaxe
Soient deux classeurs A et B
determiner une macro qui examine le contenu des cellules H4 et H5 de toutes le feuilles du classeur B
et qui va effacer dans le classeur A , feuille 1 ; des cellules au contenu identique dans les colonnes B et C ( commencant en B6 et C6) sachant que dans la colonne B ont a les valeur de la cellule H4 et dans la colonne C on a les valeur de la cellule H5
ce que j'ai fait :
dim A as workbook
dim B as workbook
dim Cell As Range
with workbook("B"].select
for each sheets range("H4"). select and range("H5").select
For Each Cell In workbook("A").Sheets("Feuil1").Range("c6:C" & [c65000].End(xlUp).Row)
If Cell = worbook("B").Sheets("").Range("H4") Then Cell.ClearContents
Next Cell
For Each Cell In workbook("A").Sheets("Feuil1").Range("B6:B" & [B65000].End(xlUp).Row)
If Cell = worbook("B").Sheets("").Range("H5") Then Cell.ClearContents
Next Cell
End Sub
merci
Bonsoir,
Et avec ce code ?
Sub Test()
Dim A As Workbook, B As Workbook
Dim xSH As Worksheet, xCell As Range
Set A = Workbooks("A.xls")
Set B = Workbooks("B.xls")
For Each xSH In B.Worksheets
For Each xCell In A.Sheets("Feuil1").Range("B6:B" & [B65000].End(xlUp).Row)
If xCell = xSH.Range("H4") Then xCell.ClearContents
Next xCell
For Each xCell In A.Sheets("Feuil1").Range("C6:C" & [C65000].End(xlUp).Row)
If xCell = xSH.Range("H5") Then xCell.ClearContents
Next xCell
Next xSH
End Sub
Merci infiniment pour ta réponse !
je voulais ajouter une condition préliminaire qui serait : " si les cellules E15 de toutes les feuilles du classeur B sont non vide
alors ...on execute le code .
puis l'ecrire ainsi en reprenant ton code ?
Sub Test()
Dim A As Workbook, B As Workbook
Dim xSH As Worksheet, xCell As Range
Set A = Workbooks("A.xls")
Set B = Workbooks("B.xls")
with B
if range("E15") ' je ne sais pas ecrire different de ' "" then
For Each xSH In B.Worksheets
For Each xCell In A.Sheets("Feuil1").Range("B21:B" & [B65000].End(xlUp).Row)
If xCell = xSH.Range("H5") Then xCell.ClearContents
Next xCell
For Each xCell In A.Sheets("Feuil1").Range("C21:C" & [C65000].End(xlUp).Row)
If xCell = xSH.Range("H4") Then xCell.ClearContents
Next xCell
Next xSH
end if
End Sub
merci pour cette relecture
(re)Bonsoir,
Code pas testé:
Sub Test()
Dim A As Workbook, B As Workbook
Dim xSH As Worksheet, xCell As Range
Dim TousNonVide As Boolean
Set A = Workbooks("GESTION DES LAISSEZ PASSER.xls")
Set B = Workbooks("LAISSEZ PASSER FRET.xls")
TousNonVide = True
For Each xSH In B.Worksheets
If xSH.Range("E15") = "" Then TousNonVide = False
Next xSH
If TousNonVide Then
For Each xSH In B.Worksheets
For Each xCell In A.Sheets("RESEAU").Range("B21:B" & [B65000].End(xlUp).Row)
If xCell = xSH.Range("H5") Then xCell.ClearContents
Next xCell
For Each xCell In A.Sheets("RESEAU").Range("C21:C" & [C65000].End(xlUp).Row)
If xCell = xSH.Range("H4") Then xCell.ClearContents
Next xCell
Next xSH
End If
End Sub
Merci pour avoir passé du temps sur mes questions , je vais prendre le temps de tester tout ca et t'en rendre compte
Bonne soirée et encor merci
.....bonjour j'ai mal formulé ma condition , parce que dans ce cas le code s'execute que si toutes les cellules E15 de toutes les feuilles du classeur sont non vide ,
il s'agit plutot d'ecrire que le code doit s'executer que pour l'ensemble des feuilles du classeur B comportent une cellule E15 non vide
( ca me fait penser à mes cours de math du coup ! avec les implications , les negations ...etc..) j'en mesure l'importance que cela a en VBA
Bonjour,
Dans ce cas, essayez le code suivant non testé:
Sub Test()
Dim A As Workbook, B As Workbook
Dim xSH As Worksheet, xCell As Range
Set A = Workbooks("GESTION DES LAISSEZ PASSER.xls")
Set B = Workbooks("LAISSEZ PASSER FRET.xls")
For Each xSH In B.Worksheets
If xSH.Range("E15") <> "" then
For Each xCell In A.Sheets("RESEAU").Range("B21:B" & [B65000].End(xlUp).Row)
If xCell = xSH.Range("H5") Then xCell.ClearContents
Next xCell
For Each xCell In A.Sheets("RESEAU").Range("C21:C" & [C65000].End(xlUp).Row)
If xCell = xSH.Range("H4") Then xCell.ClearContents
Next xCell
end if
Next xSH
End Sub
Merci infiniment !!! ca marche parfaitement !