1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| Option Explicit
Sub EcritureFichiers()
Dim LastRow As Long, i As Long, FSO As Object
Dim sFichier As String, sFeuille As String
Dim Wkb As Workbook, Wsh As Worksheet, j As Long
Dim sVal As String
sFeuille = "Feuil3"
sVal = "123456"
LastRow = ShParam.Range("B" & Rows.Count).End(xlUp).Row
If LastRow < RDepart Then Exit Sub
Application.ScreenUpdating = False
Set FSO = CreateObject("Scripting.FileSystemObject")
For i = RDepart To LastRow
sFichier = ShParam.Range("A1") & "\" & ShParam.Cells(i, 2)
If FSO.FileExists(sFichier) Then
Set Wkb = Workbooks.Open(Filename:=sFichier)
For j = 1 To Wkb.Sheets.Count
If FeuilleExiste(sFeuille, Wkb) Then
Set Wsh = Wkb.Worksheets(sFeuille)
Wsh.Cells(2, 2) = sVal
Set Wsh = Nothing
Exit For
End If
Next j
Wkb.Save
Wkb.Close
Set Wkb = Nothing
End If
Next i
Set FSO = Nothing
Application.ScreenUpdating = True
End Sub
Private Function FeuilleExiste(sNomFeuille As String, Classeur As Workbook) As Boolean
Dim Wsh As Worksheet
FeuilleExiste = False
For Each Wsh In Classeur.Worksheets
If Wsh.Name = sNomFeuille Then
FeuilleExiste = True
Exit For
End If
Next Wsh
End Function |
Partager