Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Mail_GS()
  2. Dim pvtTbl As PivotTable
  3. Dim PvtItm As PivotItem
  4. Dim Ctrl As Integer
  5. Dim PvtCtrl As PivotItem
  6. Dim SendTo As String
  7. Dim CC_list As String
  8. Dim EmailSubj As String
  9. Dim a As String
  10. Dim LTotal As Integer
  11. Dim FirstRecord As Integer
  12. Dim Counter As Integer
  13.  
  14. Set pvtTbl = Worksheets("GStocks").PivotTables("PivotTable1") 'setting pivottable
  15. Worksheets("Output Exports").Cells.Clear 'clearing export sheet. making sure it's empty
  16. pvtTbl.PivotFields("GS").ClearAllFilters 'removing any filters the pivot might have
  17. EmailSubj = Range("L3").Value 'setting to-be-sent email subject
  18. CC_list = Range("L4").Value 'setting to-be-sent email cc
  19. Counter = 0 'initializing counter var
  20.    If EmailSubj = "" Then 'setting basic subject if subject is empty
  21.        EmailSubj = "WOL PT - Encomendas LT a recuperar - " & Format(Date, "dd-mm-yyyy")
  22.     End If
  23.  
  24. 'setting everything invisible.
  25.    For Each PvtItm In pvtTbl.PivotFields("GS").PivotItems
  26.         If PvtItm = "(blank)" Then 'ignoring blank results
  27.            GoTo pop
  28.         End If
  29.         PvtItm.Visible = False
  30. pop: 'making a jump point
  31.    Next
  32.  
  33.     For Each PvtItm In pvtTbl.PivotFields("GS").PivotItems 'iterating through each item in the pivot table. we are looking at the field called "GS".
  34.        If PvtItm = "0" Or PvtItm = "(blank)" Or PvtItm = "(All)" Then
  35.             GoTo puff
  36.         End If
  37.         If PvtItm = "Sem Gestor" Then
  38.             MsgBox ("Tem encomendas sem gestor que vamos ignorar")
  39.             GoTo puff
  40.         End If
  41.         Workbooks("WOL_PT_LONGTAIL_ONL.xlsm").Activate 'making sure we have the right book active
  42.        Worksheets("Gstocks").Activate 'making sure the right sheet is active
  43.        PvtItm.Visible = True 'setting the selected item visible so the rest don't show up
  44.        LTotal = Worksheets("Gstocks").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row  'setting max lines
  45.        FirstRecord = Worksheets("Gstocks").Cells.Find(What:="SUPPLIER", SearchOrder:=xlRows, SearchDirection:=xlNext, LookIn:=xlFormulas).Row + 1 'adjusting from titlebar
  46.             'ignorar GS com 0 encomendas a recuperar.
  47.        If FirstRecord > LTotal Then
  48.             GoTo puff 'making sure if a GS has 0 orders, then we will ignore him
  49.        End If
  50.         Ctrl = MsgBox("Pretende enviar email para: " & PvtItm, vbYesNo) 'asking if the user wants to mail the output to that specific GS
  51.            'If user clicks no, we will exit cleanly.
  52.            If Ctrl = 7 Then
  53.                 GoTo puff
  54.             End If
  55.        
  56.         Worksheets("Output Exports").Cells.Clear ' clearing output again
  57.        Worksheets("GStocks").PivotTables("PivotTable1").TableRange1.Copy Destination:=Worksheets("Output Exports").Range("A1") 'copying the pivot output to the Output Exports sheet
  58.      
  59.         Worksheets("Output Exports").Activate 'activating the previous sheet
  60.        Range("J1").Value = "Nova OC" 'adding two new fields
  61.        Range("K1").Value = "Nova Previsão" 'adding two new fields
  62.        Range("I1").Select 'Selectin I1
  63.        Selection.Copy 'copying
  64.        Range("J1:K1").Select
  65.         Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
  66.              SkipBlanks:=False, Transpose:=False 'Paste as values
  67.        Application.CutCopyMode = False 'disabling cutcopymode
  68.        Columns("A:K").EntireColumn.AutoFit 'autofit (formatting the sheet)
  69.        SendTo = Range("B2").Value 'setting the TO: in the email
  70.        a = Mail_ActiveSheet(SendTo, CC_list, EmailSubj) 'calling the email function
  71.        Counter = Counter + 1 'incrementing counter with 1
  72.        
  73. puff: 'jump point for the if
  74.            If PvtItm <> "(blank)" Then 'Setting everything invisible so we can set the next item visible at the start of the cycle
  75.                PvtItm.Visible = False
  76.             End If
  77.     Next 'next iteration
  78. If Counter > 1 Then
  79. MsgBox ("Processo de envio de e-mail terminado. Enviámos: " & Counter & " e-mails.") 'showing how many emails we sent
  80. End If
  81. If Counter = 0 Then
  82. MsgBox ("Processo de envio de e-mail terminado. Nenhum e-mail enviado.") 'counter-message
  83. End If
  84. If Counter = 1 Then
  85. MsgBox ("Processo de envio de e-mails terminado. Enviámos " & Counter & " e-mail.") 'just because, if it's 1 email then it's not "emails". People are really anal.
  86. End If
  87. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement