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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
| Option Explicit
Public Const nomClasseurModele = "Macro feuille de tournée.xls"
Public Const nomClasseurRecupBase = "Test recup base.xls"
Public Const nomPrefixTournee = "Tournées du "
' VBE menu "Outils" > "Propriété de VBA Project" > "Arguments de compilation conditionnelle"
' isNetwork=1 pour un accès réseau. isNetwork=0 ou non défini pour un accès local
#If isNetwork = 1 Then
' Utilisation en réseau
Public Const nomServeur = "\\frhosclunefs1"
Public Const nomFichierRecupBase = nomServeur + "\sitcar\Distriplanner PF\" + nomClasseurRecupBase
Public Const nomFichierBaseClient = nomServeur + "\sitcar\Clients.xls"
Public Const nomFichierTournee = nomServeur + "\sitcar\Distriplanner PF\"
#Else
' Test en local
Public Const nomRepertoire = "C:\Mes documents\Christelle\Nouveau dossier\"
Public Const nomFichierRecupBase = nomRepertoire + nomClasseurRecupBase
Public Const nomFichierBaseClient = nomRepertoire + "Clients.xls"
Public Const nomFichierTournee = nomRepertoire
#End If
' A partir du fichier extrait de "Distriplanner" remplir les feuilles de tournée
Sub Tournees()
Dim dateLivraison As Date, nomClasseurTournee As String
Application.ScreenUpdating = False
'*** Récupération de la date de traitement
On Error Resume Next ' Passe en mode de gestion d'erreur manuelle avec If Err.Number <> 0 Then ...
dateLivraison = InputBox("Date de livraison (JJ/MM/AAAA)?", "Renseignement de la date")
If Err.Number <> 0 Then Avertissement "1000: La date de livraison saisie est incorrecte": Exit Sub
On Error GoTo 0 ' Restaure la gestion d'erreur automatique
nomClasseurTournee = nomPrefixTournee & _
Day(dateLivraison) & "-" & Month(dateLivraison) & "-" & Year(dateLivraison) & ".xls"
If OuvertureClasseurs(nomClasseurTournee) Then
CopieModeleClientTournee dateLivraison, nomClasseurTournee
GestionTournee dateLivraison, nomClasseurTournee
End If
Application.ScreenUpdating = False
End Sub
Function OuvertureClasseurs(ByVal nomClasseurTournee As String) As Boolean
Dim nomFichierTourneeComplet As String
OuvertureClasseurs = False
On Error Resume Next ' Passe en mode de gestion d'erreur manuelle avec If Err.Number <> 0 Then ...
'*** Ouverture du fichier avec l'extraction de base de donnéees
Workbooks.Open FileName:=nomFichierRecupBase, UpdateLinks:=0
If Err.Number <> 0 Then Avertissement "2000: Impossible d'ouvrir " + nomFichierRecupBase: Exit Function
'*** Ouverture de la base clients
Workbooks.Open FileName:=nomFichierBaseClient, UpdateLinks:=0
If Err.Number <> 0 Then Avertissement "2100: Impossible d'ouvrir " + nomFichierBaseClient: Exit Function
'*** Ouverture d'un nouveau fichier qui servira à enregistrer les feuilles de tournée
'*** et enregistrement sous le nom "Tournées du " + la date de livraison demandée
nomFichierTourneeComplet = nomFichierTournee + nomClasseurTournee
Workbooks.Add
ActiveWorkbook.SaveAs FileName:=nomFichierTourneeComplet, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
If Err.Number <> 0 Then Avertissement "2200: Impossible de sauver " + nomFichierTourneeComplet: Exit Function
On Error GoTo 0 ' Restaure la gestion d'erreur automatique
OuvertureClasseurs = True
End Function
'*** Copie des feuilles Modèle, Clients et Tournées le nouveau fichier
Sub CopieModeleClientTournee(ByVal dateLivraison As Date, ByVal nomClasseurTournee As String)
Dim feuilleCible As Worksheet
With Workbooks(nomClasseurTournee)
Set feuilleCible = .Sheets(.Sheets.Count) ' La dernière feuille
End With
Windows(nomClasseurModele).Activate
Sheets("Modèle").Select
' Sheets("Modèle").Copy After:=Workbooks(nomClasseurTournee).Sheets(3)
Sheets("Modèle").Copy After:=feuilleCible
Windows(nomClasseurModele).Activate
Sheets("Clients").Select
Sheets("Clients").Copy Before:=feuilleCible
Range("A5").Select
Range("D2").Value = "'" & dateLivraison
Windows(nomClasseurModele).Activate
Sheets("Tournées").Select
Sheets("Tournées").Copy Before:=feuilleCible
Range("A5").Select
Range("C2").Value = "'" & dateLivraison
End Sub
'*** Activation du fichier d'extraction de la base de données et début du corps de la macro
Sub GestionTournee(ByVal dateLivraison As Date, ByVal nomClasseurTournee As String)
Dim Ligne_fin As Long
Windows(nomClasseurRecupBase).Activate
Sheets("TOURS").Activate
FormatDateCol1
Sheets("RANGS").Activate
FormatDateCol1
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=dateLivraison
Selection.End(xlDown).Select
If ActiveCell.Row = 65536 Then
Avertissement "3000: Pas de magasin à livrer à cette date!"
Exit Sub
End If
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Activate
While ActiveCell <> dateLivraison
ActiveCell.Offset(1, 0).Activate
Wend
While ActiveCell.Value <> "" And ActiveCell.Value = dateLivraison
Magasin_a_Livrer dateLivraison, nomClasseurTournee
Windows(nomClasseurRecupBase).Activate
Sheets("RANGS").Activate
ActiveCell.Offset(1, 0).Activate
Wend
ActiveWindow.Close savechanges:=False
TriTourneeSauvegarde
Windows(nomClasseurModele).Activate
ActiveWindow.Close savechanges:=False
End Sub
Sub Magasin_a_Livrer(ByVal dateLivraison As Date, ByVal nomClasseurTournee As String)
Dim Magasin As String, Tonnage As Integer, Qte_EUT As Integer
Dim Rang As Long, Ligne As Long, Feuille As String
Dim Tournee As String, Quai As String, Transporteur As String, Chauffeur As String
Dim Tracteur As String, Remorque As String, Mise_a_quai As String, Date_depart As Date, Date_retour As Date
' Magasin = Mid(ActiveCell.Offset(0, 4), 2, 5) + 0
Magasin = Mid(ActiveCell.Offset(0, 4), 2, 5) + "0" ' Ajout de la chaîne "0" ?
Tournee = ActiveCell.Offset(0, 2).Value
Qte_EUT = ActiveCell.Offset(0, 7).Value
If ActiveCell.Offset(0, 8).Value = "" Then
Tonnage = 0
Else
Tonnage = ActiveCell.Offset(0, 8).Value * 1000
End If
Rang = ActiveCell.Offset(0, 3).Value
Windows(nomClasseurTournee).Activate
Sheets("Tournées").Select
Range("A5").Select
Selection.AutoFilter Field:=1, Criteria1:=Tournee
Selection.End(xlDown).Select
Ligne = ActiveCell.Row
Selection.AutoFilter
If Ligne = 65536 Then ' La tournée n'a pas été trouvée
Selection.End(xlUp).Select
Windows(nomClasseurRecupBase).Activate
Sheets("TOURS").Select
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=dateLivraison
Selection.AutoFilter Field:=3, Criteria1:=Tournee ' Double filtre
Selection.End(xlDown).Select
Quai = ActiveCell.Offset(0, 3).Value
Mise_a_quai = ActiveCell.Offset(0, 4).Value
Date_depart = ActiveCell.Offset(0, 5).Value
Date_retour = ActiveCell.Offset(0, 6).Value
Tracteur = ActiveCell.Offset(0, 7).Value
Chauffeur = ActiveCell.Offset(0, 8).Value
Remorque = ActiveCell.Offset(0, 9).Value
Transporteur = ActiveCell.Offset(0, 10).Value
AjoutTournee dateLivraison, nomClasseurTournee, _
Tournee, Quai, Transporteur, Chauffeur, Tracteur, _
Remorque, Mise_a_quai, Date_depart, Date_retour
End If
Windows(nomClasseurTournee).Activate
Feuille = "" & Tournee
Sheets(Feuille).Select
ActiveCell.Value = Magasin
ActiveCell.Offset(0, 4).Value = Qte_EUT
ActiveCell.Offset(0, 5).Value = Tonnage
ActiveCell.Offset(0, 22).Value = Rang
ActiveCell.Offset(1, 0).Activate
Sheets("Clients").Select
ActiveCell.Offset(1, 0).Activate
ActiveCell.Value = "'" & Mise_a_quai
ActiveCell.Offset(0, 1).Value = "'" & Date_depart
ActiveCell.Offset(0, 2).Value = Tournee
ActiveCell.Offset(0, 3).Value = Transporteur
ActiveCell.Offset(0, 4).Value = Magasin
ActiveCell.Offset(0, 7).Value = Quai
End Sub
Sub AjoutTournee(ByVal dateLivraison As Date, ByVal nomClasseurTournee As String, _
ByVal Tournee As String, ByVal Quai As String, ByVal Transporteur As String, _
ByVal Chauffeur As String, ByVal Tracteur As String, ByVal Remorque As String, _
ByVal Mise_a_quai As String, ByVal Date_depart As Date, ByVal Date_retour As Date)
Dim Position As Long, Feuille As String
Windows(nomClasseurTournee).Activate
Sheets.Add
ActiveSheet.Name = Tournee
Sheets("Modèle").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Feuille = "" & Tournee
Sheets(Feuille).Select
ActiveSheet.Paste
ActiveWindow.Zoom = 50
Application.CutCopyMode = False
Range("A1:Z25").Select
Range("Z25").Activate
Application.CutCopyMode = False
configurationImpression
Range("B11").Select
Range("G2").Value = "TOURNEE " & Tournee
Range("W2").Value = "QUAI: " & Quai
Range("C4").Value = Transporteur
Range("C5").Value = Chauffeur
Range("C6").Value = Tracteur
Range("C7").Value = Remorque
Range("G5").Value = "'" & dateLivraison
DateDeMiseAQuai Mise_a_quai
Range("B11").Select
Sheets("Tournées").Select
Range("A5").Select
Selection.End(xlDown).Select
Position = ActiveCell.Row
If Position = 65536 Then
Range("A6").Select
Else
If Position = 6 Then
Selection.End(xlDown).Select
Position = ActiveCell.Row
If Position = 65536 Then
Range("A7").Select
Else
ActiveCell.Offset(1, 0).Activate
End If
Else
ActiveCell.Offset(1, 0).Activate
End If
End If
ActiveCell.Value = Tournee
ActiveCell.Offset(0, 1).Value = Quai
ActiveCell.Offset(0, 2).Value = Chauffeur
ActiveCell.Offset(0, 3).Value = Transporteur
ActiveCell.Offset(0, 4).Value = Tracteur
ActiveCell.Offset(0, 5).Value = Remorque
ActiveCell.Offset(0, 6).Value = "'" & Mise_a_quai
ActiveCell.Offset(0, 8).Value = "'" & Date_depart
ActiveCell.Offset(0, 10).Value = "'" & Date_retour
End Sub
Sub DateDeMiseAQuai(ByVal Mise_a_quai As String)
Dim Jour As String, Mois As String
If Mise_a_quai = "" Then
Range("Y6").Value = "'" & Mise_a_quai
Else
If Day(Mise_a_quai) < 10 Then
Jour = "0" & Day(Mise_a_quai)
Else
Jour = Day(Mise_a_quai)
End If
If Month(Mise_a_quai) < 10 Then
Mois = "0" & Month(Mise_a_quai)
Else
Mois = Month(Mise_a_quai)
End If
Range("W6").Value = "'" & Jour & "/" & Mois & "/" & Year(Mise_a_quai)
End If
End Sub
Sub TriTourneeSauvegarde()
Dim Feuille As String, Ligne_fin As Long
Sheets("Tournées").Select
Range("A6").Select
While ActiveCell.Value <> ""
Feuille = "" & ActiveCell.Value
Sheets(Feuille).Select
Range("B11:X23").Select
Selection.Sort Key1:=Range("X11"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Columns("X:X").Delete
Range("B11").Select
Sheets("Tournées").Select
ActiveCell.Offset(1, 0).Activate
Wend
Range("A6").Select
Sheets("Clients").Select
Range("A6").Select
Selection.End(xlDown).Select
Ligne_fin = ActiveCell.Row
Range("A6:J" & Ligne_fin).Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1"), _
Order2:=xlAscending, Key3:=Range("B1"), Order3:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("A6").Select
ActiveWorkbook.Save
End Sub
Sub FormatDateCol1()
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"
Range("A1").Select
End Sub
Sub configurationImpression()
ActiveSheet.PageSetup.PrintArea = "$A$1:$AC$25"
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PrintHeadings = False
.PrintGridlines = False
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
.Draft = False
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End Sub
Sub Avertissement(ByVal strMsg As String)
Const nbrChiffreCodeErreur = 4 ' Nombre de chiffre dans le code d'erreur. Exemple :"1000: message"
MsgBox Mid(strMsg, nbrChiffreCodeErreur + 3), vbExclamation, _
"Tournées : avertissement n° " + Left(strMsg, nbrChiffreCodeErreur)
End Sub |
Partager