+ Reply to Thread
Results 1 to 1 of 1

Looping a pivot filter change & sending via email

  1. #1
    Registered User
    Join Date
    07-08-2014
    Location
    Birmingham, England
    MS-Off Ver
    2003
    Posts
    1

    Looping a pivot filter change & sending via email

    Hi all

    First time posting, i'm wondering if anyone can spot where i am going wrong.

    I have a pivot table with info on a number of buildings. The page filter is set up to select each building in turn. I also have a list of personel on a different tab with their email adresses.

    What i am trying to do is:

    Filter the pivot table one building at a time, copy the filtered pivot into a new workbook, save the workbook as the building name, open a new email, attach the saved workbook, name the subject as the building name, and email the workbook to the associated email adresses.

    I then want to loop the macro to work its way down the list of sites, sending emails to the relevant personnel.

    By probably blind luck, the macro i have created works all the way through, but fails at the first sign of looping. Can anyone help please?





    Sub Macro1()
    '
    Dim x As Integer

    Sheets("Email").Select
    ' Set numrows = number of rows of data.
    NumRows = Range("b2", Range("b2").End(xlDown)).Rows.Count
    ' Select cell a1.
    Range("b2").Select
    ' Establish "For" loop to loop "numrows" number of times.
    For x = 1 To NumRows

    Selection.Copy
    Sheets("Fault Report").Select
    Range("F1").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

    Sheets("Fault Report").PivotTables("PivotTable1").PivotFields("Site").CurrentPage _
    = Sheets("Fault Report").Range("F1").Value

    Sheets("Fault Report").Select
    Selection.Copy
    Application.CutCopyMode = False
    Sheets("Fault Report").Select
    Sheets("Fault Report").Copy

    ChDir "C:\Documents and Settings\firstname.lastname\Desktop"

    FolderPath = "C:\Documents and Settings\firstname.lastname\Desktop\"
    SavePath = FolderPath & Sheets("Fault Report").Range("F1").Text
    ActiveWorkbook.SaveAs Filename:=SavePath, FileFormat:=xlNormal

    Dim OL As Object

    Dim EmailItem As Object
    Dim Doc As Workbook
    Application.ScreenUpdating = False

    Set OL = CreateObject("Outlook.Application")
    Set EmailItem = OL.CreateItem(olMailItem)

    Set Doc = ActiveWorkbook

    With EmailItem

    .Subject = Sheets("Fault Report").Range("F1").Value

    .To = "[email protected];"
    .Importance = olImportanceNormal 'Or olImprotanceHigh Or olImprotanceLow
    .Attachments.Add Doc.FullName
    .Display

    ' .Send

    ActiveWorkbook.Close

    End With

    Application.ScreenUpdating = True

    Set Doc = Nothing

    Set OL = Nothing

    Set EmailItem = Nothing

    ' Selects cell down 1 row from active cell.
    ActiveCell.Offset(1, 0).Select
    Next

    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] change font colour in outlook when sending email using vba
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-20-2014, 09:55 AM
  2. Drop down box to change stock amount and email sending
    By exzel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-05-2013, 08:30 AM
  3. Pivot Table, Looping through Filter
    By noobtime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2013, 05:53 PM
  4. Change Pivot table Filter Based on Cell Value *Multiple Filter items* Possible?
    By Flydd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2012, 06:57 AM
  5. Change Case of recipients name when sending email
    By marcusjb in forum Outlook Programming / VBA / Macros
    Replies: 3
    Last Post: 02-03-2012, 09:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1