Option Explicit
Dim ch As String

Public Sub ANouveau_Bouton()
Dim Adjust, Button
Set Button = ActiveSheet.Buttons.Add(Left:=Adjust, Top:=100, Width:=70, Height:=30)
ActiveSheet.Shapes("Bouton 1").Select
    Selection.Characters.Text = "Rfrence"
    With Selection.Characters(Start:=1, Length:=100).Font
        .Name = "Vijaya"
        .FontStyle = "Normal"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
    End With
    Range("A1").Select
Button.OnAction = "Nouveau_Fichier"
ch = ActiveWorkbook.Path & "\"
 
End Sub
Public Sub Nouveau_Fichier()
Dim wb, ws2, onglet1, Derligne, i, Adjust, Bouton, Bouton2, ws1
 
Cells.Select
Selection.Copy 'copie toutes les donnes du fichier global
Set wb = Workbooks.Add
' ws2 feuille extrait client
Set ws2 = wb.Worksheets(1) 'cre un nouveau fichier et un nouvel onglet
Set onglet1 = Sheets.Add
onglet1.Name = "Rfrence" 'nomme l'onglet
ActiveSheet.Paste 'colle les donnes du fichier global sur le nouveau fichier
Rows(1).Insert
Rows(1).Insert
Range("D1").Select
ActiveCell.FormulaR1C1 = "Period from 01.01.2014 to 03.31.2014"
Range("D2").Select
ActiveCell.FormulaR1C1 = "Priode du 01.01.2014 au 31.03.2014"
With onglet1
.Columns(1).Insert
        Derligne = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 4 To Derligne
            If .Cells(i, 12) <= 2 And .Cells(i, 13) <= 4 Then .Cells(i, 1) = 0
            If .Cells(i, 12) > 2 Or .Cells(i, 13) > 4 Then .Cells(i, 1) = 1
        Next
End With
 

Columns("B:B").Select
Selection.Copy
Columns("A:A").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A3").Select
ActiveCell.Value = "Service Status"
Columns("B:B").Delete Shift:=xlToLeft
Columns("E:E").Delete Shift:=xlToLeft
Columns("E:E").Delete Shift:=xlToLeft
Columns("F:F").Delete Shift:=xlToLeft
Range("B3").Select
ActiveCell.FormulaR1C1 = ("Applicant / Donneur d'Ordre")
Range("C3").Select
ActiveCell.FormulaR1C1 = ("Applicant name /nom du Donneur d'Ordre")
Range("D3").Select
ActiveCell.FormulaR1C1 = ("CPUid number / numro CPUid")
Range("E3").Select
ActiveCell.FormulaR1C1 = ("last Alcatel-Lucent PO / dernier numro de commande Alcatel Lucent")
Range("F3").Select
ActiveCell.FormulaR1C1 = ("Order type / type de commande")
Range("G3").Select
ActiveCell.FormulaR1C1 = ("Date of the last order / Date de la dernire commande")
Range("H3").Select
ActiveCell.FormulaR1C1 = ("Qty of IP Trunk / Nbre d'IP Trunk")
Range("I3").Select
ActiveCell.FormulaR1C1 = ("Qty of IP users / Nbre d'utilisateur IP")

Set Bouton = ActiveSheet.Buttons.Add(Left:=Adjust, Top:=Adjust, Width:=70, Height:=30)
ActiveSheet.Shapes("Button 1").Select
    Selection.Characters.Text = "Rcapitulatif"
    With Selection.Characters(Start:=1, Length:=100).Font
        .Name = "Vijaya"
        .FontStyle = "Normal"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
    End With
Bouton.OnAction = "extract"

Set Bouton2 = ActiveSheet.Buttons.Add(Left:=75, Top:=Adjust, Width:=70, Height:=30)
    ActiveSheet.Shapes("Button 2").Select
    Selection.Characters.Text = "Fichier DO"
    With Selection.Characters(Start:=1, Length:=100).Font
        .Name = "Vijaya"
        .FontStyle = "Normal"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
    End With
Bouton2.OnAction = "somme"

Range("A1").Select
 

ws2.Name = "Otoc_service_" & Format(Now, "dd-mm-yyyy") & ".xlsm" 'insrer la date voulue
wb.SaveAs ch & ws2.Name & ".xlsx"
Application.ScreenUpdating = True
Set ws1 = Nothing
Set ws2 = Nothing
Application.StatusBar = "Traitement termin"
End Sub
Public Sub extract()
Dim ws1, ws2, i, plc, clr, ctriptrunk, ctripusers, nplc, recapexist
' ws1=feuille rfrence
Set ws1 = Worksheets("Rfrence")

recapexist = True
'  partir d'ici, en cas d'erreur on va  terreur (voir plus bas)
On Error GoTo terreur
' ws2=feuille rcapitulatif (si la feuille n'exsite pas, il y aura une erreur
Set ws2 = Worksheets("Rcapitulatif")
'  partir d'ici, en cas d'erreur on laisse faire le traitement d'erreur par dfaut
On Error GoTo 0
' si la feuille n'existe pas
If recapexist = False Then
  Set ws2 = Worksheets.Add 'cre la feuille
  ws2.Name = ("Rcapitulatif") 'nomme la feuille
End If
'sur la feuille rcap, on met l'entete des colonnes
ws2.Cells.Delete
ws2.Range("A1") = "Applicant / Donneur d'ordre"
ws2.Range("B1") = "Applicant name : Nom du donneur d'ordre"
ws2.Range("C1") = "Qty IP Trunk / Qt IP Trunk"
ws2.Range("D1") = "Service IP Trunk"
ws2.Range("E1") = "Qty IP Users / Qt IP Users"
ws2.Range("F1") = "Service IP Users"

' on boucle sur les lignes clients
' i ligne client en cours
i = 4
' premire ligne avec numro de client en cours
plc = i
'clr compteur de ligne client sur le rcapitulatif
clr = 1
' ctr = totaux iptrunk et ipusers
ctriptrunk = 0
ctripusers = 0
' nplc = numro de client en cours
nplc = ws1.Cells(plc, 2)
While ws1.Cells(i, 2) <> ""
  ' si numro de client trouv sur la ligne <> numro de client en cours
  If ws1.Cells(i, 2) <> nplc Then
   clr = clr + 1
   ws1.Range("B" & plc & ":c" & plc).Copy ws2.Range("a" & clr)
   ws2.Range("C" & clr) = ctriptrunk
   ws2.Range("D" & clr) = "3EY98995AA"
   ws2.Range("E" & clr) = ctripusers
   ws2.Range("F" & clr) = "3EY98994AA"
   ctriptrunk = 0
   ctripusers = 0
   plc = i
   nplc = ws1.Cells(i, 2)
  End If
  ' si le status est <> 0 on ajoute les quantits aux totaux
  If ws1.Range("A" & i) <> 0 Then
   ctriptrunk = ctriptrunk + ws1.Range("H" & i)
   ctripusers = ctripusers + ws1.Range("I" & i)
  End If
  ' on prend la ligne client suivante
  i = i + 1
Wend
' traitement pour le dernier client
clr = clr + 1
ws1.Range("B" & plc & ":c" & plc).Copy ws2.Range("a" & clr)
ws2.Range("C" & clr) = ctriptrunk
ws2.Range("D" & clr) = "3EY98995AA"
ws2.Range("E" & clr) = ctripusers
ws2.Range("F" & clr) = "3EY98994AA"
' trace le cadre
With ws2.Range("A1:F" & clr)
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
' ajuste la taille des caractres
.Font.Size = 8
' ajuste la taille des lignes et des colonnes
.EntireColumn.AutoFit
.EntireRow.AutoFit
End With

Range("A1").Select

Set ws1 = Nothing
Set ws2 = Nothing
Exit Sub
terreur:
recapexist = False
Resume Next
End Sub
Public Sub somme()
Dim chemin, ws1, i, nplc, ws2, wb, j
Application.ScreenUpdating = False
' ws1= reference  la feuille des clients
Set ws1 = Worksheets(1)
' i pointeur de ligne dans la feuille clients
i = 4
' nplc client en cours de traitement
nplc = ""
' on parcourt la feuille des clients
While ws1.Cells(i, "B") <> 0

 ' si le numro de client sur la ligne i est diffrent du client en cours
If ws1.Cells(i, "B") <> nplc Then
    ' si client en cours est non blanc
    If nplc <> "" Then
      wb.SaveAs ch & ws2.Name & ".xlsx"
      wb.Close
    End If
    ' on cre un nouveau classeur extrait client
    ' wb classeur extrait client
   Set wb = Workbooks.Add
    ' ws2 feuille extrait client
   Set ws2 = wb.Worksheets(1)
    ' nplc = client en cours
   nplc = ws1.Cells(i, "B")
    Application.StatusBar = "Client " & nplc & " en cours de cration"
    ws2.Name = ("OTOC_Service_") & nplc & ("_") & Format(Date, "dd-mm-yyyy")
    ' on copie la ligne
    ws1.Range("A3:I3").Copy ws2.Range("A1")
    ' j pointeur de ligne dans le classeur extrait client
    j = 1

  End If
  ' si on n'a pas le statut = 0 sur cette ligne
  If Not (ws1.Range("A" & i)) = 0 Then
    ' incrmente pointeur de ligne
    j = j + 1
    'on copie la ligne
    ws1.Rows(i).Copy ws2.Range("A" & j)
    Range("H" & j + 1 & ":I" & j + 1).Formula = "=SUM(H2:H" & j & ")"
 End If
   
' on passe  la ligne client suivante
i = i + 1
Wend

Range("A1").Select


' on ferme le dernier classeur
wb.SaveAs ch & ws2.Name & ".xlsx"
wb.Close
Application.ScreenUpdating = True
Set ws1 = Nothing
Set ws2 = Nothing
Application.StatusBar = "traitement termin"
End Sub