Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Mail_GS()
- Dim pvtTbl As PivotTable
- Dim PvtItm As PivotItem
- Dim Ctrl As Integer
- Dim PvtCtrl As PivotItem
- Dim SendTo As String
- Dim CC_list As String
- Dim EmailSubj As String
- Dim a As String
- Dim LTotal As Integer
- Dim FirstRecord As Integer
- Dim Counter As Integer
- Set pvtTbl = Worksheets("GStocks").PivotTables("PivotTable1") 'setting pivottable
- Worksheets("Output Exports").Cells.Clear 'clearing export sheet. making sure it's empty
- pvtTbl.PivotFields("GS").ClearAllFilters 'removing any filters the pivot might have
- EmailSubj = Range("L3").Value 'setting to-be-sent email subject
- CC_list = Range("L4").Value 'setting to-be-sent email cc
- Counter = 0 'initializing counter var
- If EmailSubj = "" Then 'setting basic subject if subject is empty
- EmailSubj = "WOL PT - Encomendas LT a recuperar - " & Format(Date, "dd-mm-yyyy")
- End If
- 'setting everything invisible.
- For Each PvtItm In pvtTbl.PivotFields("GS").PivotItems
- If PvtItm = "(blank)" Then 'ignoring blank results
- GoTo pop
- End If
- PvtItm.Visible = False
- pop: 'making a jump point
- Next
- For Each PvtItm In pvtTbl.PivotFields("GS").PivotItems 'iterating through each item in the pivot table. we are looking at the field called "GS".
- If PvtItm = "0" Or PvtItm = "(blank)" Or PvtItm = "(All)" Then
- GoTo puff
- End If
- If PvtItm = "Sem Gestor" Then
- MsgBox ("Tem encomendas sem gestor que vamos ignorar")
- GoTo puff
- End If
- Workbooks("WOL_PT_LONGTAIL_ONL.xlsm").Activate 'making sure we have the right book active
- Worksheets("Gstocks").Activate 'making sure the right sheet is active
- PvtItm.Visible = True 'setting the selected item visible so the rest don't show up
- LTotal = Worksheets("Gstocks").Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row 'setting max lines
- FirstRecord = Worksheets("Gstocks").Cells.Find(What:="SUPPLIER", SearchOrder:=xlRows, SearchDirection:=xlNext, LookIn:=xlFormulas).Row + 1 'adjusting from titlebar
- 'ignorar GS com 0 encomendas a recuperar.
- If FirstRecord > LTotal Then
- GoTo puff 'making sure if a GS has 0 orders, then we will ignore him
- End If
- Ctrl = MsgBox("Pretende enviar email para: " & PvtItm, vbYesNo) 'asking if the user wants to mail the output to that specific GS
- 'If user clicks no, we will exit cleanly.
- If Ctrl = 7 Then
- GoTo puff
- End If
- Worksheets("Output Exports").Cells.Clear ' clearing output again
- Worksheets("GStocks").PivotTables("PivotTable1").TableRange1.Copy Destination:=Worksheets("Output Exports").Range("A1") 'copying the pivot output to the Output Exports sheet
- Worksheets("Output Exports").Activate 'activating the previous sheet
- Range("J1").Value = "Nova OC" 'adding two new fields
- Range("K1").Value = "Nova Previsão" 'adding two new fields
- Range("I1").Select 'Selectin I1
- Selection.Copy 'copying
- Range("J1:K1").Select
- Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
- SkipBlanks:=False, Transpose:=False 'Paste as values
- Application.CutCopyMode = False 'disabling cutcopymode
- Columns("A:K").EntireColumn.AutoFit 'autofit (formatting the sheet)
- SendTo = Range("B2").Value 'setting the TO: in the email
- a = Mail_ActiveSheet(SendTo, CC_list, EmailSubj) 'calling the email function
- Counter = Counter + 1 'incrementing counter with 1
- puff: 'jump point for the if
- If PvtItm <> "(blank)" Then 'Setting everything invisible so we can set the next item visible at the start of the cycle
- PvtItm.Visible = False
- End If
- Next 'next iteration
- If Counter > 1 Then
- MsgBox ("Processo de envio de e-mail terminado. Enviámos: " & Counter & " e-mails.") 'showing how many emails we sent
- End If
- If Counter = 0 Then
- MsgBox ("Processo de envio de e-mail terminado. Nenhum e-mail enviado.") 'counter-message
- End If
- If Counter = 1 Then
- 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.
- End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement