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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| Public DernierLigne As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (x As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (x As Currency) As Boolean
Option Explicit
Dim TabFichiers() As String
Dim Dep As Currency, Fin As Currency, Freq As Currency
Dim n As Long, bFlagNomFeuille As Boolean
Dim iRow As Long, Cpt As Long, NbFichiers As Long, sNum As String, sNomAct As String
' A Adapter
Const sNomFeuilleALire As String = "ESSAICIS"
Const iRowDep As Long = 1
Const sNomFeuillesDatas As String = "Datas_"
Const TypeFichier As String = "xls"
Private Sub DelFeuilles()
Dim Ws As Worksheet
For Each Ws In ThisWorkbook.Worksheets
If (Ws.Name <> Feuil1.Name) And (Ws.Name Like sNomFeuillesDatas & "*") Then
Application.DisplayAlerts = False
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
Private Function ExistenceNomFeuille(sNomFichier As String, sNomFeuille As String) As Boolean
Dim Conn As Object
Dim Cat As Object
Dim Feuille As Object
Dim sNom As String
Set Conn = CreateObject("ADODB.Connection")
Set Cat = CreateObject("ADOX.Catalog")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sNomFichier & ";" & _
"Extended Properties=""Excel 8.0;"""
Set Cat.ActiveConnection = Conn
ExistenceNomFeuille = False
For Each Feuille In Cat.Tables
Select Case Right$(Feuille.Name, 1)
Case "$"
sNom = Left$(Feuille.Name, Len(Feuille.Name) - 1)
If sNom = sNomFeuille Then
ExistenceNomFeuille = True
Exit For
End If
Case "'"
sNom = Mid$(Feuille.Name, 2, Len(Feuille.Name) - 3)
If sNom = sNomFeuille Then
ExistenceNomFeuille = True
Exit For
End If
End Select
Next Feuille
Conn.Close
Set Cat = Nothing
Set Conn = Nothing
End Function
Private Sub Init()
iRow = iRowDep: Cpt = 0: NbFichiers = 0: n = 0: sNum = ""
DelFeuilles
End Sub
Private Sub LectureFichiers()
Dim i As Long
For i = 1 To UBound(TabFichiers)
Lire TabFichiers(i)
Cpt = Cpt + 1
Next i
End Sub
Private Sub Lire(ByVal sNomFichier As String)
Dim FSO As Object
Dim Fichier As String
Dim LastRow As Long
Dim Wkb As Workbook, sNomSh As String
Dim LastRowPaste As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Fichier = FSO.GetFileName(sNomFichier)
Application.DisplayAlerts = False
Set Wkb = Application.Workbooks.Open(sNomFichier, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=True)
Application.DisplayAlerts = True
LastRow = Wkb.Sheets(sNomFeuilleALire).Range("A" & Rows.Count).End(xlUp).Row
LastRowPaste = iRow + LastRow - iRowDep
If LastRowPaste > Feuil1.Rows.Count Or sNum = "" Then
iRow = iRowDep
sNomSh = ThisWorkbook.ActiveSheet.Name
ThisWorkbook.Sheets.Add
n = n + 1
Select Case n
Case 1 To 9: sNum = "00" & CStr(n)
Case 10 To 99: sNum = "0" & CStr(n)
Case Else: sNum = CStr(n)
End Select
With ThisWorkbook
.ActiveSheet.Name = sNomFeuillesDatas & sNum
.ActiveSheet.Move After:=.Worksheets(sNomSh)
.ActiveSheet.Range("A" & iRow).Select
ActiveWindow.FreezePanes = False
End With
sNomAct = sNomFeuillesDatas & sNum
End If
' Plage à Copier et donc à adapter
'Wkb.Sheets(sNomFeuilleALire).Range("A18:H49" & LastRow).Copy
Wkb.Sheets(sNomFeuilleALire).Range("A18:H49").Copy
ThisWorkbook.Worksheets(sNomAct).Range("A" & iRow).PasteSpecial xlPasteValues 'fonctionne
'ThisWorkbook.Worksheets(sNomAct).Range("A18", Range("H" & ActiveCell.Row).End(xlToLeft)).PasteSpecial xlPasteValues
'Range(Range("A" & ActiveCell.Row), Range("IV" & ActiveCell.Row).End(xlToLeft)).Select 'support micro$oft
'iRow = iRow + LastRow
'iRow = iRow + Range("A18:H49")
IdentDernierLigne
iRow = DernierLigne
With Application
.StatusBar = "Lecture Fichiers : " & Cpt + 1 & " / " & NbFichiers
.CutCopyMode = False
End With
Wkb.Close False
Set FSO = Nothing
End Sub
Private Function IdentDernierLigne()
Dim c As Range
Set c = ThisWorkbook.Worksheets(sNomAct).Range("A:A").Find("*", , xlValues, , , xlPrevious)
If Not c Is Nothing Then
DernierLigne = c.Row + 1
Set c = Nothing
End If
End Function
Private Sub ListeFichiersDossier(sChemin As String, bInclureSousDossiers As Boolean)
Dim FSO As Object, Dossier As Object, Fichier As String
Dim sPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Dossier = FSO.GetFolder(sChemin)
Fichier = Dir$(sChemin & "\*.*")
Do While Len(Fichier) > 0
sPath = sChemin & "\" & Fichier
If Fichier <> ThisWorkbook.Name And UCase(TypeFichier) = UCase(FSO.GetExtensionName(Fichier)) Then
NbFichiers = NbFichiers + 1
ReDim Preserve TabFichiers(1 To NbFichiers)
TabFichiers(NbFichiers) = sPath
End If
Fichier = Dir$()
Loop
If bInclureSousDossiers Then
For Each Dossier In Dossier.SubFolders
ListeFichiersDossier Dossier.Path, True
Next Dossier
End If
Set Dossier = Nothing
Set FSO = Nothing
End Sub
Private Sub MepFeuilles()
Dim Ws As Worksheet
For Each Ws In ThisWorkbook.Worksheets
If (Ws.Name <> Feuil1.Name) And (Ws.Name Like sNomFeuillesDatas & "*") Then
IdentDernierLigne
With Ws
.Activate
.Tab.ColorIndex = 20
.Range("A" & DernierLigne, "K" & DernierLigne + 100).Cells.Delete
.Columns("A:Y").Columns.AutoFit
.Rows("1:1").Insert Shift:=xlDown
.Range("A1") = "Nom"
.Range("B1") = "Epaisseur joint"
.Range("C1") = "Date fabrication"
.Range("D1") = "Date essai"
.Range("E1") = "Largeur"
.Range("F1") = "Longeur"
.Range("G1") = "Force"
.Range("H1") = "Contrainte"
.Columns("C:D").NumberFormat = "mm/dd/yyyy"
.Range("H:H").NumberFormat = "0.0"
.Range("I1") = "Datation"
.Range("I2").FormulaR1C1 = "=LEFT(RIGHT(RC[-8],5),3)"
.Range("I2").AutoFill Destination:=Range("I2:I1000")
.Range("A1").AutoFilter
.Range("A1:I" & DernierLigne).Sort Key1:=Range("I1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
End If
Next Ws
End Sub
Private Sub MepFinale()
With ActiveWindow
.ScrollRow = 1
.ScrollColumn = 1
End With
With Feuil1
.Activate
.Range("C1").Select
End With
End Sub
Sub SelDossier()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ThisWorkbook.Path & "\"
.Title = "Dossier à traiter"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.ButtonName = "Sélection Dossier"
.Show
If .SelectedItems.Count > 0 Then
QueryPerformanceCounter Dep
Init
DoEvents
Application.ScreenUpdating = False
ListeFichiersDossier .SelectedItems(1), False
If NbFichiers = 0 Then
MepFinale
Application.ScreenUpdating = True
MsgBox "Pas de fichier xls dans " & .SelectedItems(1), vbOKOnly + vbInformation, "Infos"
Exit Sub
End If
TestExistenceFeuilleDossier
If bFlagNomFeuille = False Then
MepFinale
Application.ScreenUpdating = True
Exit Sub
End If
LectureFichiers
MepFeuilles
MepFinale
QueryPerformanceCounter Fin: QueryPerformanceFrequency Freq
With Application
.ScreenUpdating = True
.StatusBar = "Terminé : Fichiers " & Cpt & " / " & Format(((Fin - Dep) / Freq), "0.00 s")
End With
End If
With Feuil1
.Activate
.Range("C1").Select
End With
End With
End Sub
Private Sub TestExistenceFeuilleDossier()
Dim i As Long, sNomFichier As String
Dim iMax As Long
iMax = UBound(TabFichiers)
bFlagNomFeuille = True
For i = LBound(TabFichiers) To UBound(TabFichiers)
bFlagNomFeuille = ExistenceNomFeuille(TabFichiers(i), sNomFeuilleALire)
If bFlagNomFeuille = False Then
Application.ScreenUpdating = True
sNomFichier = TabFichiers(i)
MsgBox "La feuille " & sNomFeuilleALire & " n'existe pas dans " & vbCrLf & sNomFichier, vbOKOnly + vbInformation
Exit Sub
End If
Application.StatusBar = "Vérification existence : " & sNomFeuilleALire & " " & i & " / " & iMax
Next i
End Sub |
Partager