Compréhension et modification de code
r
Bonjour le forum,
On m'a donné un code afin d'ouvrir une feuille et traiter ses différentes colonnes afin de l'envoyer à ses clients.
Étant donné mon nombre de feuille plutôt conséquent je souhaite automatiser tout ça, j'ai fais ceci pour l'instant :
Sub ouvrir_onglets()
Application.ScreenUpdating = True
Dim myPath As String, myFile As Variant
Dim wkbk As Workbook
myPath = "C:\Users\XXXXXX\Desktop\2020-07-01\"
myFile = Dir(myPath & "\*.xls*")
c = 2 'ligne
Do While myFile <> ""
Cells(c, 1) = myFile 'colonne
Set wkbk = Workbooks.Open(myPath & myFile)
wkbk.Activate
wkbk.ActiveSheet.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
wkbk.Close SaveChanges:=False
myFile = Dir()
c = c + 1 'ligne + 1
Loop
End Sub
afin d'ouvrir tous les onglets pour pouvoir les traiter ensuite.
Voici le code avec lequel je voudrais le "mixer", il fait peur mais j'ai besoin de chaque partie de ce code, à part celui de l'ouverture des fichiers :
Sub macro1()
Dim intChoice As Integer
Dim strPath As String
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
"Fichiers CSV seulement", "*.csv")
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
Dim fso As Object
Dim fileName As String
Dim filePath As String
Set fso = CreateObject("Scripting.FileSystemObject")
fileName = fso.GetFileName(strPath)
filePath = Left(strPath, InStrRev(strPath, "\"))
Dim filewoext As String
filewoext = Left(fileName, InStr(fileName, "."))
Const xlDelimited = 1
Const xlNormal = -4143
Dim Excel
Set Excel = CreateObject("Excel.Application")
With Excel
.Workbooks.Open strPath
.Sheets(1).Columns("A").TextToColumns .Range("A1"), xlDelimited, , , , , True
.ActiveWorkbook.SaveAs .ActiveWorkbook.Path & "\" & filewoext & "xlsx", FileFormat:=xlOpenXMLWorkbook
.Quit
End With
Dim wkbk As Workbook
Dim sht As Worksheet
NewPath = filePath & filewoext & "xlsx"
NewFileName = filewoext & "xlsx"
If strPath <> "" Then
Workbooks.Open NewPath
End If
Set wkbk = Workbooks(NewFileName)
Set sht = wkbk.Sheets(1)
Set cwk = Workbooks("GenFactureOVH.xlsm")
Set csht = cwk.Sheets(1)
Dim g As Long
Dim h As Long
Dim hdate As String
Dim newdate As String
Dim newtime As String
Dim newdatetime As String
h = 2
Dim yr, mh, dy, hr, mn, sc As Integer
Application.CutCopyMode = False
sht.Columns("A").Insert XlDirection.xlToRight
sht.Columns("A").Value = sht.Columns("C").Value
sht.Columns("C").Delete
sht.Range("A:A").Select
Selection.NumberFormat = "@"
Selection.Replace "datetime", "Jour & Heure d'appel"
For g = sht.UsedRange.Rows.Count To 1 Step -1
hdate = sht.Range("A" & h).Value
If hdate <> "" Then
yr = CInt(Left(hdate, 4))
mh = CInt(Mid(hdate, 5, 2))
dy = CInt(Mid(hdate, 7, 2))
hr = CInt(Mid(hdate, 9, 2))
mn = CInt(Mid(hdate, 11, 2))
sc = CInt(Mid(hdate, 13, 2))
newdate = DateValue(DateSerial(yr, mh, dy))
newtime = TimeSerial(hr, mn, sc)
newdatetime = newdate & " " & newtime
sht.Range("A" & h).NumberFormat = "@"
sht.Range("A" & h).Value = newdatetime
End If
h = h + 1
Next
sht.Columns("I:N").EntireColumn.Delete
sht.Range("B:B,D:D").Select
Selection.Replace "PhoneLine", "Numéro emetteur"
Selection.Replace "calledNumber", "Numéro appelé"
Selection.Replace "33", "0"
Selection.NumberFormat = "0#"" ""##"" ""##"" ""##"" ""##"
sht.Range("C:C,E:E,F:F,G:G,H:H").Select
Selection.Replace "duration", "Durée de l'appel"
Selection.Replace "nature", "Nature"
Selection.Replace "type", "Type"
Selection.Replace "priceWithOutVAT", "Prix H.T. OVH"
Selection.Replace "destination", "Destination"
Selection.Replace "landLine", "national"
Selection.Replace "OVH VoIP", "VoIP"
Application.CutCopyMode = False
sht.Columns("I").Insert XlDirection.xlToRight
sht.Range("I1").Value = "Prix H.T."
sht.Range("I:I").Select
Selection.NumberFormat = "0.000"
Dim k As Long
Dim l As Long
Dim ComptFax As Long
l = 2
ComptFax = 0
For k = sht.UsedRange.Rows.Count To 1 Step -1
If sht.Range("C" & l).Value <> "" Then
If sht.Range("F" & l).Value = "national" Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (csht.Range("C2").Value / 60), 3)
End If
If sht.Range("C" & l).Value >= 3600 And sht.Range("E" & l).Value = "national" And sht.Range("F" & l) = "national" Then
sht.Range("I" & l).Value = sht.Range("H" & l).Value
End If
If sht.Range("E" & l).Value = "transfert" And sht.Range("F" & l).Value = "mobile" Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (csht.Range("E2").Value / 60), 3)
End If
If sht.Range("E" & l).Value = "national" And sht.Range("F" & l).Value = "mobile" Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (csht.Range("D2").Value / 60), 3)
End If
If sht.Range("E" & l).Value = "international" And sht.Range("F" & l).Value = "mobile" Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (0.1 / 60), 3)
sht.Range("F" & l).Value = "mobile international"
End If
If sht.Range("E" & l).Value = "international" And sht.Range("F" & l).Value = "national" Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (0 / 60), 3)
sht.Range("F" & l).Value = "fixe international"
End If
If sht.Range("B" & l).Value = csht.Range("F2").Value And sht.Range("E" & l).Value = "national" And sht.Range("F" & l).Value = "national" Then
If csht.Range("G2").Value = "Forfait Appel" Then
ComptFax = ComptFax + 1
If ComptFax <= csht.Range("I2").Value Then
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (0 / 60), 3)
sht.Range("F" & l).Value = "fax forfait"
Else
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (Range("H2").Value / 60), 3)
sht.Range("F" & l).Value = "fax hors-forfait"
End If
Else
sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (Range("H2").Value / 60), 3)
sht.Range("F" & l).Value = "fax"
End If
End If
If sht.Range("B" & l).Value = csht.Range("F2").Value And sht.Range("F" & l).Value = "special" Then
sht.Range("I" & l).Value = sht.Range("H" & l).Value
sht.Range("F" & l).Value = "fax special"
End If
If sht.Range("E" & l).Value = "national" And sht.Range("F" & l).Value = "special" Then
' sht.Range("I" & l).Value = Round(sht.Range("C" & l).Value * (0.01 / 60), 3)
sht.Range("I" & l).Value = sht.Range("H" & l).Value
End If
l = l + 1
End If
Next
Dim i As Long
Dim j As Long
j = 2
For i = sht.UsedRange.Rows.Count To 1 Step -1
If sht.Range("C" & j).Value <> "" Then
sht.Range("C" & j).Value = Format(TimeSerial(0, 0, sht.Range("C" & j).Value), "hh:mm:ss")
j = j + 1
End If
Next
wkbk.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"R1C1:R1048576C9", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="R2C11", _
TableName:="Synthese", DefaultVersion:= _
xlPivotTableVersion15
sht.Select
sht.Cells(2, 11).Select
With sht.PivotTables("Synthese").PivotFields("Type")
.Orientation = xlRowField
.Position = 1
End With
sht.PivotTables("Synthese").AddDataField sht. _
PivotTables("Synthese").PivotFields("Durée de l'appel"), _
"Nombre de Durée de l'appel", xlCount
sht.PivotTables("Synthese").AddDataField sht. _
PivotTables("Synthese").PivotFields("Prix H.T."), _
"Nombre de Prix H.T.", xlCount
With sht.PivotTables("Synthese").PivotFields( _
"Nombre de Durée de l'appel")
.Caption = "Somme de Durée de l'appel"
.Function = xlSum
End With
With sht.PivotTables("Synthese").PivotFields( _
"Nombre de Prix H.T.")
.Caption = "Somme de Prix H.T."
.Function = xlSum
End With
sht.Range("L3:L10").Select
Selection.NumberFormat = "[h]:mm:ss;@"
sht.Range("A1:I1").Select
Selection.Font.Bold = True
sht.Columns("A:I").EntireColumn.AutoFit
sht.PivotTables("Synthese").PivotFields("Somme de Durée de l'appel"). _
Caption = "Durée totale des appels"
sht.PivotTables("Synthese").PivotFields("Somme de Prix H.T."). _
Caption = "Total Prix H.T."
sht.Range("K2").Select
sht.PivotTables("Synthese").CompactLayoutRowHeader = "Type d'appel"
wkbk.ShowPivotTableFieldList = False
ActiveWindow.LargeScroll ToRight:=-1
wkbk.ActiveSheet.Copy After:=ThisWorkbook.Sheets(Sheets.Count)
wkbk.Close SaveChanges:=False
End Sub
Merci beaucoup, bon courage à ceux qui m'aideront et bonne journée à tous !
r
Re le forum, je me permets de faire remonter mon topic étant toujours bloqué, mercii
Bonjour,
Vu la longueur conséquence du code que l'on t'a donné, et vu l'absence de réponse ...
- soit tu fais appel au concepteur du code
- soit tu postes un exemple de fichier et tu expliques ce que tu souhaites ... ce sera plus facule sans doute de repartir d'une feuille blanche est des outils de chacun