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
| 'Lit un fichier de données de revision et alimente un tableau de données
'Paramètres : - nom complet du fichier
' : - le tableau à alimenter
Function Lecture_fich_revision(ByVal nomFichierComplet As String, ByRef tableau() As Variant) As Boolean
Dim intFic As Integer, i As Integer
Dim strligne As String, codeCompte As String
Dim signe As Integer
Lecture_fich_revision = False
intFic = FreeFile
'Ouverture fichier
On Error Resume Next
Open nomFichierComplet For Input As intFic
If Err.Number <> 0 Then
MsgBox "Erreur lors de l'ouverture du fichier " & nomFichierComplet & vbCrLf & Err.Description
Err.Clear
Exit Function
End If
On Error GoTo 0
'Lecture fichier
i = 1
While Not EOF(intFic)
Line Input #intFic, strligne
codeCompte = Trim(Mid(strligne, 12, 13))
'Filtre sur les comptes 6 et 7
If (Left(codeCompte, 1) = "6" Or Left(codeCompte, 1) = "7") Then
ReDim Preserve tableau(1 To 10, 1 To i)
tableau(1, i) = CLng(Trim(Mid(strligne, 1, 11))) 'n° de ligne
tableau(2, i) = codeCompte 'code compte
tableau(3, i) = Trim(Mid(strligne, 25, 44)) 'libellé compte
tableau(4, i) = CByte(Trim(Mid(strligne, 69, 2))) 'jour
tableau(5, i) = CByte(Trim(Mid(strligne, 80, 2))) 'mois
tableau(6, i) = CInt(Trim(Mid(strligne, 89, 4))) 'année
tableau(7, i) = Trim(Mid(strligne, 93, 35)) 'libellé ecriture
tableau(8, i) = DateSerial(Trim(Mid(strligne, 134, 4)), Trim(Mid(strligne, 131, 2)), Trim(Mid(strligne, 128, 2))) 'date debut MAL GEREE
tableau(9, i) = DateSerial(Trim(Mid(strligne, 164, 4)), Trim(Mid(strligne, 161, 2)), Trim(Mid(strligne, 158, 2))) 'date fin MAL GEREE
signe = -2 * CInt(Trim(Mid(strligne, 198, 1))) + 1 'signe montant
tableau(10, i) = signe * CDbl(Trim(Mid(strligne, 199, 20))) 'montant
i = i + 1
End If
Wend
'Fermeture fichier
On Error Resume Next
Close intFic
If Err.Number <> 0 Then
MsgBox "Erreur lors de la fermeture du fichier " & nomFichierComplet & vbCrLf & Err.Description
Err.Clear
Exit Function
End If
On Error GoTo 0
Lecture_fich_revision = True
End Function
Sub Remplit_feuille_data(ByVal nomComplFich As String)
Dim data() As Variant
Dim n As Long, m As Long
'Si la lecture a échoué on sort
If (Not Lecture_fich_revision(nomComplFich, data)) Then Exit Sub
'Sinon on alimente la feuille données et on la met en forme
n = UBound(data, 2)
m = UBound(data, 1)
With ThisWorkbook.Worksheets("DONNEES")
.Range(.Cells(2, 1), .Cells(.Rows.Count, .Columns.Count)).ClearContents
.Range(.Cells(2, 1), .Cells(.Rows.Count, .Columns.Count)).ClearFormats
.Range(.Cells(2, m - 2), .Cells(n + 1, m - 1)).NumberFormat = "m/d/yyyy"
.Range(.Cells(2, m + 1), .Cells(n + 1, m + 1)).NumberFormat = "m/d/yyyy"
.Range(.Cells(2, 1), .Cells(n + 1, m)).Value = Application.Transpose(data)
.Range(.Cells(2, m + 1), .Cells(n + 1, m + 1)).FormulaR1C1 = "=DATEVALUE(RC[-7]&""/""&RC[-6]&""/""&RC[-5])"
.Range(.Cells(2, m + 2), .Cells(n + 1, m + 2)).FormulaR1C1 = "=RC[-3]-RC[-4]+1"
.Range(.Cells(2, m + 3), .Cells(n + 1, m + 3)).FormulaR1C1 = "=IF(RC[-7]<YEAR(FinPeriode),"""",IF(RC[-2]>FinPeriode,""CAP"",""CCA""))"
.Range(.Cells(2, m + 4), .Cells(n + 1, m + 4)).FormulaR1C1 = "=MAX(MIN(FinPeriode,RC[-5])+1-RC[-6],0)"
.Range(.Cells(2, m + 5), .Cells(n + 1, m + 5)).FormulaR1C1 = "=MAX(RC[-6]-MAX(RC[-7],FinPeriode),0)"
.Range(.Cells(2, m + 6), .Cells(n + 1, m + 6)).FormulaR1C1 = "=ROUND(IF(RC[-3]=""CAP"",RC[-6]/RC[-4]*RC[-2],IF(RC[-3]=""CCA"",-RC[-6]/RC[-4]*RC[-1],0)),2)"
.Range(.Cells(2, m + 7), .Cells(n + 1, m + 7)).FormulaR1C1 = "=IF(LEFT(RC[-15],1)=""6"",RC[-4],IF(LEFT(RC[-15],1)=""7"",IF(RC[-4]=""CAP"",""PCA"",IF(RC[-4]=""CCA"",""PCA"","""")),""""))"
.Calculate
.Columns.AutoFit
End With
End Sub |
Partager