VBA to Split data into several reports and email via outlook

Skjoldborg

New Member
Joined
Feb 16, 2009
Messages
7
Hello VBA Expers :)

Six months ago I startet looking into VBA, and this forum basically taught me all I know of VBA

I have always been able to find answers here.. However here is my first post on a problem I can’t solve on my own or seem to read the answer to.

Here is the case:
I am extracting data from a excel file and then I am arranging the data in my own excel file. My own excel file contains “contracts” - each line in this sheet contains a contract. Within the line I have all the information needed. I want to create several reports with contracts - they have to be split depending on who owns the contract. The contract owner is defined within each line of the data. The “contract owner” can have multiple contracts so I need to sort the reports so that each "contract owner" only gets 1 email with and attachment with 1 excel file with all his contracts.

So basically I want to split my excel file into several reports depending on a certain criteria (cell value = contract owner).

And… I basically created a code that does all this for me.. however I need to tweak the code.

This is important, as you can see in my code, it is long and depending on the names within the VBA code are correct.
(Right now i have 46 owners and 250 contracts, so i would have to doublicate the code below 46 times!!).

Further I am possiblely not the one to maintain the VBA code - so when contract owners change (promted/hired/fired) the code needs to be update manually.. which is a huge pitfall. (or impossible with the code in its current form).

This is were you come in! There are so many experts out there, I am sure one of you can crack this case – and enlighten me :)

Code:
Sub splitnsend()
    'Here i select the first contract owner
    Selection.AutoFilter Field:=14, Criteria1:="Bill Gates"
    Columns("A:O").Select
    Selection.Copy
    
    'creating a new workbook that will serve as report (later to be emailed to bill)
    Workbooks.Add
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit
    Application.CutCopyMode = False
    Selection.Interior.ColorIndex = xlNone
    
    'following code is the report on Bills active contracts
    '' THE FOLLOWING IS CREATING THE REPORT
    Range("A1").Select
    RowCount = ActiveCell.CurrentRegion.Rows.Count
    
    ActiveCell.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    
    ActiveCell.Offset(2, 0).FormulaR1C1 = "Total Contract Value (SEK)"
    ActiveCell.Offset(3, 0).FormulaR1C1 = "Total Commitment (SEK)"
    
    ActiveCell.Offset(2, 1).FormulaR1C1 = "=SUM(OFFSET(R1C3,0,0,COUNTA(C3),1))"
    ActiveCell.Offset(3, 1).FormulaR1C1 = "=SUM(OFFSET(R1C4,0,0,COUNTA(C4),1))"
    
    ActiveCell.Offset(3, 1).Select
    ActiveCell.CurrentRegion.Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
    
    Selection.Font.Bold = True
    Range("A1").Select
    ''REPORT DONE - TIME TO EMAIL:
    Range("O2").Select 'selecting range for emailaddress
    
    'Defining stuff for email
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim email_ As String
    Dim subject_ As String
    Dim body_ As String
    Dim rCell As Range
    Dim EmailStr As String
     'Create  Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    
     'Create list of emails from selected cells
     'THIS is a code is what i use to define my "to" (this case bill)
     'I KNOW this is not what the code originally intended to do.. it was
     'designed to create a list of emailaddresses (from a selected range)
    For Each rCell In Selection.Cells
        EmailStr = EmailStr + rCell & ";"
    Next rCell
    
    'save attachment
    'I need to save the workbook i am gonna report in order to give it the name i want
    ActiveWorkbook.SaveAs Filename:= _
        Environ$("temp") & "\" & ActiveWorkbook.ActiveSheet.Range("N2").Value & "'s" & " " & "Consultant Report" & ".xls" _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
         
    email_ = EmailStr
    subject_ = "Consultant Contract Report"
    body_ = "Hello, Attached you find a report of your currently active contracts"
    
     'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem
        .To = email_
        .Subject = subject_
        .Attachments.Add ActiveWorkbook.FullName
        .Body = body_
        .Display
    End With
    
    ActiveWorkbook.Close
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
    End With
    
    Windows("Split and send.xls").Activate
    Sheets("Sheet1").Select
    Selection.AutoFilter Field:=14
    
    '**** NEW OWNER****
    ' THEN I START ALL OVER WITH STEVE's CONTRACTS
    ' I HAVE 46 contract owners in my orignial datas
    ' I really hope there is a better way - or it is gonna be
    ' a long a vounaruble code
    Selection.AutoFilter Field:=14, Criteria1:="Steve Jobs"
    Columns("A:O").Select
    Selection.Copy
    
    Workbooks.Add
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit
    Application.CutCopyMode = False
    Selection.Interior.ColorIndex = xlNone
    
    Range("A1").Select
    RowCount = ActiveCell.CurrentRegion.Rows.Count
    
    ActiveCell.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    
    ActiveCell.Offset(2, 0).FormulaR1C1 = "Total Contract Value (SEK)"
    ActiveCell.Offset(3, 0).FormulaR1C1 = "Total Commitment (SEK)"
    
    ActiveCell.Offset(2, 1).FormulaR1C1 = "=SUM(OFFSET(R1C3,0,0,COUNTA(C3),1))"
    ActiveCell.Offset(3, 1).FormulaR1C1 = "=SUM(OFFSET(R1C4,0,0,COUNTA(C4),1))"
    
    ActiveCell.Offset(3, 1).Select
    ActiveCell.CurrentRegion.Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
    
    Selection.Font.Bold = True
    Range("A1").Select
    
    Range("O2").Select
    
     'Create  Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    
     'Create list of emails from selected cells
    For Each rCell In Selection.Cells
        EmailStr = EmailStr + rCell & ";"
    Next rCell
    
    'save attachment
    ActiveWorkbook.SaveAs Filename:= _
        Environ$("temp") & "\" & ActiveWorkbook.ActiveSheet.Range("N2").Value & "'s" & " " & "Consultant Report" & ".xls" _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
         
    email_ = EmailStr
    subject_ = "Consultant Contract Report"
    body_ = "Hello, Attached you find a report of your currently active contracts"
    
     'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem
        .To = email_
        .Subject = subject_
        .Attachments.Add ActiveWorkbook.FullName
        .Body = body_
        .Display
    End With
    
    ActiveWorkbook.Close
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
    End With
    
    Windows("Split and send.xls").Activate
    Sheets("Sheet1").Select
    Selection.AutoFilter Field:=14
    
     '**** NEW OWNER****
     ' STARTING OVER AGAIN *SIGH* :)
    'Selection.AutoFilter Field:=14, Criteria1:="Steve Jobs"
    '.............
    '.......
    '....
    '...

Hope to see some inputs - thank you in advace. Peace out! :)

(Before you suggest I put this into Access I have to add that I know nothing of Access and my department insists to keep our database as an excel-sheet until a proper contract management system is in place)

//Skjoldborg
VBA student
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I would set up another sheet and have a named range containing all the people's names. I would have another column that status - either "ACTIVE" / "INACTIVE". An "ACTIVE" person is somebody that still works for the company, an "INACTIVE" one has left.

At the outset, I would read these names and their status into arrays. I would then LOOP through the array of names - and if their status is ACTIVE then perform your code. Thanks

Kaps
 
Upvote 0
Hey Kaps!
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
Thank you for your email.<o:p></o:p>
<o:p> </o:p>
I did guess that maybe loop would be useful. However I am not sure I fully understand how that works, and my case is pretty complicated (in my opinion). Further I haven’t really tried programming with loop before (I am pretty novice). <o:p></o:p>
<o:p> </o:p>
So I was up for quite a challenge to select unique values and copy a range to a new workbook. At least I found it very difficult to see how to make the link between sort, copy and loop. So since you posted I have been trying to understand how that works.<o:p></o:p>
<o:p> </o:p>
In my search I came upon another forum post that suggested me to look at http://www.rondebruin.nl/sendmail.htm<o:p></o:p>
<o:p> </o:p>
This page is absolutely AWESOME. Here I found something that fits perfectly to my needs. BIG BIG credits to that website.<o:p></o:p>
<o:p> </o:p>
Below you will find the solution that I have chosen. This selects unique references in a datasheet and copy all relevant data references (by sorting unique) and posting in a new temporary workbook. The temp workbook will be send to the relevant person with a text I have chosen.<o:p></o:p>
<o:p> </o:p>
I have customized the original code a bit – I have included my manipulation of the temp workbook and change file name to include cell.value<o:p></o:p>
<o:p> </o:p>
Rich (BB code):
<o:p></o:p>
Rich (BB code):
Rich (BB code):
Option Explicit<o:p></o:p>
<o:p> </o:p>
Sub ConsultantReport()<o:p></o:p>
    Dim OutApp As Object<o:p></o:p>
    Dim OutMail As Object<o:p></o:p>
    Dim rng As Range<o:p></o:p>
    Dim Ash As Worksheet<o:p></o:p>
    Dim Cws As Worksheet<o:p></o:p>
    Dim Rcount As Long<o:p></o:p>
    Dim Rnum As Long<o:p></o:p>
    Dim FilterRange As Range<o:p></o:p>
    Dim FieldNum, Rowcount As Integer<o:p></o:p>
    Dim mailAddress As String<o:p></o:p>
    Dim NewWB As Workbook<o:p></o:p>
    Dim TempFilePath As String<o:p></o:p>
    Dim TempFileName As String<o:p></o:p>
    Dim FileExtStr As String<o:p></o:p>
    Dim FileFormatNum As Long<o:p></o:p>
<o:p> </o:p>
    On Error GoTo cleanup<o:p></o:p>
    Set OutApp = CreateObject("Outlook.Application")<o:p></o:p>
    OutApp.Session.Logon<o:p></o:p>
<o:p> </o:p>
    With Application<o:p></o:p>
        .EnableEvents = False<o:p></o:p>
        .ScreenUpdating = False<o:p></o:p>
    End With<o:p></o:p>
    <o:p></o:p>
    'Set filter sheet<o:p></o:p>
    Set Ash = Workbooks("<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Split</st1:place></st1:City> and send.xls").Sheets("Reportdata")<o:p></o:p>
<o:p> </o:p>
    'Set filter range and filter column (column with names)<o:p></o:p>
    Set FilterRange = Ash.Range("A1:O" & Ash.Rows.Count)<o:p></o:p>
    FieldNum = 1    'Filter column = A because the filter range start in column A<o:p></o:p>
<o:p> </o:p>
    'Add a worksheet for the unique list and copy the unique list in A1<o:p></o:p>
    Set Cws = Worksheets.Add<o:p></o:p>
    FilterRange.Columns(FieldNum).AdvancedFilter _<o:p></o:p>
            Action:=xlFilterCopy, _<o:p></o:p>
            CopyToRange:=Cws.Range("A1"), _<o:p></o:p>
            CriteriaRange:="", Unique:=True<o:p></o:p>
<o:p> </o:p>
    'Count of the unique values + the header cell<o:p></o:p>
    Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))<o:p></o:p>
<o:p> </o:p>
    'If there are unique values start the loop<o:p></o:p>
    If Rcount >= 2 Then<o:p></o:p>
        For Rnum = 2 To Rcount<o:p></o:p>
<o:p> </o:p>
            'Look for the mail address in the MailInfo worksheet<o:p></o:p>
            mailAddress = ""<o:p></o:p>
            On Error Resume Next<o:p></o:p>
            mailAddress = Application.WorksheetFunction. _<o:p></o:p>
                          VLookup(Cws.Cells(Rnum, 1).Value, _<o:p></o:p>
                                  Worksheets("Mailinfo").Range("A1:B" & _<o:p></o:p>
                                                               Worksheets("Mailinfo").Rows.Count), 2, False)<o:p></o:p>
            On Error GoTo 0<o:p></o:p>
<o:p> </o:p>
            If mailAddress <> "" Then<o:p></o:p>
<o:p> </o:p>
                'Filter the FilterRange on the FieldNum column<o:p></o:p>
                FilterRange.AutoFilter Field:=FieldNum, _<o:p></o:p>
                                       Criteria1:=Cws.Cells(Rnum, 1).Value<o:p></o:p>
<o:p> </o:p>
                'Copy the visible data in a new workbook<o:p></o:p>
                With Ash.AutoFilter.Range<o:p></o:p>
                    On Error Resume Next<o:p></o:p>
                    Set rng = .SpecialCells(xlCellTypeVisible)<o:p></o:p>
                    On Error GoTo 0<o:p></o:p>
                End With<o:p></o:p>
<o:p> </o:p>
                Set NewWB = Workbooks.Add(xlWBATWorksheet)<o:p></o:p>
<o:p> </o:p>
                rng.Copy<o:p></o:p>
                With NewWB.Sheets(1)<o:p></o:p>
                    .Cells(1).PasteSpecial Paste:=8<o:p></o:p>
                    .Cells(1).PasteSpecial Paste:=xlPasteValues<o:p></o:p>
                    .Cells(1).PasteSpecial Paste:=xlPasteFormats<o:p></o:p>
                    .Cells(1).Select<o:p></o:p>
                    Application.CutCopyMode = False<o:p></o:p>
                End With<o:p></o:p>
<o:p> </o:p>
                'Create a file name<o:p></o:p>
                TempFilePath = Environ$("temp") & "\"<o:p></o:p>
                TempFileName = "Consultant Report " & ActiveWorkbook.ActiveSheet.Range("A2").Value & " " & Format(Now, "dd-mmm-yy")<o:p></o:p>
                                <o:p></o:p>
                If Val(Application.Version) < 12 Then<o:p></o:p>
                    'You use Excel 2000-2003<o:p></o:p>
                    FileExtStr = ".xls": FileFormatNum = -4143<o:p></o:p>
                Else<o:p></o:p>
                    'You use Excel 2007<o:p></o:p>
                    FileExtStr = ".xlsx": FileFormatNum = 51<o:p></o:p>
                End If<o:p></o:p>
                ’’**************<o:p></o:p>
                ‘This is my “extra” manipulating with the data<o:p></o:p>
                 ‘ I do this in each Temp file. <o:p></o:p>
                Columns("A:B").Select<o:p></o:p>
                Selection.Copy<o:p></o:p>
                Range("P1").Select<o:p></o:p>
                ActiveSheet.Paste<o:p></o:p>
<o:p> </o:p>
                Columns("A:B").Select<o:p></o:p>
                Selection.Delete Shift:=xlToLeft<o:p></o:p>
                <o:p></o:p>
                Rows("1:1").Select<o:p></o:p>
                Selection.Font.Bold = True<o:p></o:p>
                <o:p></o:p>
                Range("A1").Select<o:p></o:p>
                Rowcount = ActiveCell.CurrentRegion.Rows.Count<o:p></o:p>
    <o:p></o:p>
                ActiveCell.End(xlDown).Select<o:p></o:p>
                ActiveCell.Offset(1, 0).Select<o:p></o:p>
    <o:p></o:p>
                ActiveCell.Offset(2, 0).FormulaR1C1 = "Total Contract Value (SEK)"<o:p></o:p>
                ActiveCell.Offset(3, 0).FormulaR1C1 = "Total Commitment (SEK)"<o:p></o:p>
    <o:p></o:p>
                ActiveCell.Offset(2, 1).FormulaR1C1 = "=SUM(OFFSET(R1C3,0,0,COUNTA(C3),1))"<o:p></o:p>
                ActiveCell.Offset(3, 1).FormulaR1C1 = "=SUM(OFFSET(R1C4,0,0,COUNTA(C4),1))"<o:p></o:p>
    <o:p></o:p>
                ActiveCell.Offset(3, 1).Select<o:p></o:p>
                ActiveCell.CurrentRegion.Select<o:p></o:p>
                Selection.Style = "Comma"<o:p></o:p>
                Selection.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"<o:p></o:p>
    <o:p></o:p>
                Selection.Font.Bold = True<o:p></o:p>
                Range("A1").Select<o:p></o:p>
<o:p> </o:p>
                'Save, Mail, Close and Delete the file<o:p></o:p>
                Set OutMail = OutApp.CreateItem(0)<o:p></o:p>
<o:p> </o:p>
                With NewWB<o:p></o:p>
                'The workbook will be saved with a name including the owner<o:p></o:p>
                'hence there can be no / or \ or other illegal characthers in the owner name<o:p></o:p>
                    .SaveAs TempFilePath & TempFileName _<o:p></o:p>
                          & FileExtStr, FileFormat:=FileFormatNum<o:p></o:p>
<o:p> </o:p>
                    On Error Resume Next<o:p></o:p>
                    With OutMail<o:p></o:p>
                        .SentOnBehalfOfName = "me@mail.dk"<o:p></o:p>
                        .to = mailAddress<o:p></o:p>
                        .Subject = "Consultant Report"<o:p></o:p>
                        .Attachments.Add NewWB.FullName<o:p></o:p>
                        .Body = "Hello," & Chr(13) & Chr(13) _<o:p></o:p>
                        & "Attached you will find a report of your currently active contract(s)" & Chr(13) _<o:p></o:p>
                        & "Please send us your feedback if the attached information is incorrect or needs an update." & Chr(13) _<o:p></o:p>
                        & "Feedback and questions can be directed to me@mail.dk." & Chr(13) & Chr(13) _<o:p></o:p>
                        & "Best regards," & Chr(13) & "Group Services" & Chr(13)<o:p></o:p>
                        .Display<o:p></o:p>
                    End With<o:p></o:p>
                    On Error GoTo 0<o:p></o:p>
                    .Close savechanges:=False<o:p></o:p>
                End With<o:p></o:p>
<o:p> </o:p>
                Set OutMail = Nothing<o:p></o:p>
                Kill TempFilePath & TempFileName & FileExtStr<o:p></o:p>
            End If<o:p></o:p>
<o:p> </o:p>
            'Close AutoFilter<o:p></o:p>
            Ash.AutoFilterMode = False<o:p></o:p>
<o:p> </o:p>
        Next Rnum<o:p></o:p>
    End If<o:p></o:p>
<o:p> </o:p>
cleanup:<o:p></o:p>
    Set OutApp = Nothing<o:p></o:p>
    Application.DisplayAlerts = False<o:p></o:p>
    Cws.Delete<o:p></o:p>
    Application.DisplayAlerts = True<o:p></o:p>
<o:p> </o:p>
    With Application<o:p></o:p>
        .EnableEvents = True<o:p></o:p>
        .ScreenUpdating = True<o:p></o:p>
    End With<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>

<o:p> </o:p>
I have a workbook with data and macro that works if anybody ever needs to test it out.<o:p></o:p>
PM me for the workbook (I have no rights to upload).<o:p></o:p>
<o:p> </o:p>
B.R.<o:p></o:p>
Skjoldborg<o:p></o:p>
 
Upvote 0
Hey Kaps!
<o:p></o:p>
Thank you for your email.<o:p></o:p>
<o:p></o:p>
I did guess that maybe loop would be useful. However I am not sure I fully understand how that works, and my case is pretty complicated (in my opinion). Further I haven’t really tried programming with loop before (I am pretty novice). <o:p></o:p>
<o:p></o:p>
So I was up for quite a challenge to select unique values and copy a range to a new workbook. At least I found it very difficult to see how to make the link between sort, copy and loop. So since you posted I have been trying to understand how that works.<o:p></o:p>
<o:p></o:p>
In my search I came upon another forum post that suggested me to look at http://www.rondebruin.nl/sendmail.htm<o:p></o:p>
<o:p></o:p>
This page is absolutely AWESOME. Here I found something that fits perfectly to my needs. BIG BIG credits to that website.<o:p></o:p>
<o:p></o:p>
Below you will find the solution that I have chosen. This selects unique references in a datasheet and copy all relevant data references (by sorting unique) and posting in a new temporary workbook. The temp workbook will be send to the relevant person with a text I have chosen.<o:p></o:p>
<o:p></o:p>
I have customized the original code a bit – I have included my manipulation of the temp workbook and change file name to include cell.value<o:p></o:p>
<o:p></o:p>
Rich (BB code):
<o:p></o:p>
Rich (BB code):
Option Explicit<o:p></o:p>
<o:p></o:p>
Sub ConsultantReport()<o:p></o:p>
   Dim OutApp As Object<o:p></o:p>
   Dim OutMail As Object<o:p></o:p>
   Dim rng As Range<o:p></o:p>
   Dim Ash As Worksheet<o:p></o:p>
   Dim Cws As Worksheet<o:p></o:p>
   Dim Rcount As Long<o:p></o:p>
   Dim Rnum As Long<o:p></o:p>
   Dim FilterRange As Range<o:p></o:p>
   Dim FieldNum, Rowcount As Integer<o:p></o:p>
   Dim mailAddress As String<o:p></o:p>
   Dim NewWB As Workbook<o:p></o:p>
   Dim TempFilePath As String<o:p></o:p>
   Dim TempFileName As String<o:p></o:p>
   Dim FileExtStr As String<o:p></o:p>
   Dim FileFormatNum As Long<o:p></o:p>
<o:p></o:p>
   On Error GoTo cleanup<o:p></o:p>
   Set OutApp = CreateObject("Outlook.Application")<o:p></o:p>
   OutApp.Session.Logon<o:p></o:p>
<o:p></o:p>
   With Application<o:p></o:p>
       .EnableEvents = False<o:p></o:p>
       .ScreenUpdating = False<o:p></o:p>
   End With<o:p></o:p>
   <o:p></o:p>
   'Set filter sheet<o:p></o:p>
   Set Ash = Workbooks("<st1:City w:st="on"><st1:place w:st="on">Split</st1:place></st1:City> and send.xls").Sheets("Reportdata")<o:p></o:p>
<o:p></o:p>
   'Set filter range and filter column (column with names)<o:p></o:p>
   Set FilterRange = Ash.Range("A1:O" & Ash.Rows.Count)<o:p></o:p>
   FieldNum = 1    'Filter column = A because the filter range start in column A<o:p></o:p>
<o:p></o:p>
   'Add a worksheet for the unique list and copy the unique list in A1<o:p></o:p>
   Set Cws = Worksheets.Add<o:p></o:p>
   FilterRange.Columns(FieldNum).AdvancedFilter _<o:p></o:p>
           Action:=xlFilterCopy, _<o:p></o:p>
           CopyToRange:=Cws.Range("A1"), _<o:p></o:p>
           CriteriaRange:="", Unique:=True<o:p></o:p>
<o:p></o:p>
   'Count of the unique values + the header cell<o:p></o:p>
   Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))<o:p></o:p>
<o:p></o:p>
   'If there are unique values start the loop<o:p></o:p>
   If Rcount >= 2 Then<o:p></o:p>
       For Rnum = 2 To Rcount<o:p></o:p>
<o:p></o:p>
           'Look for the mail address in the MailInfo worksheet<o:p></o:p>
           mailAddress = ""<o:p></o:p>
           On Error Resume Next<o:p></o:p>
           mailAddress = Application.WorksheetFunction. _<o:p></o:p>
                         VLookup(Cws.Cells(Rnum, 1).Value, _<o:p></o:p>
                                 Worksheets("Mailinfo").Range("A1:B" & _<o:p></o:p>
                                                              Worksheets("Mailinfo").Rows.Count), 2, False)<o:p></o:p>
           On Error GoTo 0<o:p></o:p>
<o:p></o:p>
           If mailAddress <> "" Then<o:p></o:p>
<o:p></o:p>
               'Filter the FilterRange on the FieldNum column<o:p></o:p>
               FilterRange.AutoFilter Field:=FieldNum, _<o:p></o:p>
                                      Criteria1:=Cws.Cells(Rnum, 1).Value<o:p></o:p>
<o:p></o:p>
               'Copy the visible data in a new workbook<o:p></o:p>
               With Ash.AutoFilter.Range<o:p></o:p>
                   On Error Resume Next<o:p></o:p>
                   Set rng = .SpecialCells(xlCellTypeVisible)<o:p></o:p>
                   On Error GoTo 0<o:p></o:p>
               End With<o:p></o:p>
<o:p></o:p>
               Set NewWB = Workbooks.Add(xlWBATWorksheet)<o:p></o:p>
<o:p></o:p>
               rng.Copy<o:p></o:p>
               With NewWB.Sheets(1)<o:p></o:p>
                   .Cells(1).PasteSpecial Paste:=8<o:p></o:p>
                   .Cells(1).PasteSpecial Paste:=xlPasteValues<o:p></o:p>
                   .Cells(1).PasteSpecial Paste:=xlPasteFormats<o:p></o:p>
                   .Cells(1).Select<o:p></o:p>
                   Application.CutCopyMode = False<o:p></o:p>
               End With<o:p></o:p>
<o:p></o:p>
               'Create a file name<o:p></o:p>
               TempFilePath = Environ$("temp") & "\"<o:p></o:p>
               TempFileName = "Consultant Report " & ActiveWorkbook.ActiveSheet.Range("A2").Value & " " & Format(Now, "dd-mmm-yy")<o:p></o:p>
                               <o:p></o:p>
               If Val(Application.Version) < 12 Then<o:p></o:p>
                   'You use Excel 2000-2003<o:p></o:p>
                   FileExtStr = ".xls": FileFormatNum = -4143<o:p></o:p>
               Else<o:p></o:p>
                   'You use Excel 2007<o:p></o:p>
                   FileExtStr = ".xlsx": FileFormatNum = 51<o:p></o:p>
               End If<o:p></o:p>
               ’’**************<o:p></o:p>
               ‘This is my “extra” manipulating with the data<o:p></o:p>
                ‘ I do this in each Temp file. <o:p></o:p>
               Columns("A:B").Select<o:p></o:p>
               Selection.Copy<o:p></o:p>
               Range("P1").Select<o:p></o:p>
               ActiveSheet.Paste<o:p></o:p>
<o:p></o:p>
               Columns("A:B").Select<o:p></o:p>
               Selection.Delete Shift:=xlToLeft<o:p></o:p>
               <o:p></o:p>
               Rows("1:1").Select<o:p></o:p>
               Selection.Font.Bold = True<o:p></o:p>
               <o:p></o:p>
               Range("A1").Select<o:p></o:p>
               Rowcount = ActiveCell.CurrentRegion.Rows.Count<o:p></o:p>
   <o:p></o:p>
               ActiveCell.End(xlDown).Select<o:p></o:p>
               ActiveCell.Offset(1, 0).Select<o:p></o:p>
   <o:p></o:p>
               ActiveCell.Offset(2, 0).FormulaR1C1 = "Total Contract Value (SEK)"<o:p></o:p>
               ActiveCell.Offset(3, 0).FormulaR1C1 = "Total Commitment (SEK)"<o:p></o:p>
   <o:p></o:p>
               ActiveCell.Offset(2, 1).FormulaR1C1 = "=SUM(OFFSET(R1C3,0,0,COUNTA(C3),1))"<o:p></o:p>
               ActiveCell.Offset(3, 1).FormulaR1C1 = "=SUM(OFFSET(R1C4,0,0,COUNTA(C4),1))"<o:p></o:p>
   <o:p></o:p>
               ActiveCell.Offset(3, 1).Select<o:p></o:p>
               ActiveCell.CurrentRegion.Select<o:p></o:p>
               Selection.Style = "Comma"<o:p></o:p>
               Selection.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"<o:p></o:p>
   <o:p></o:p>
               Selection.Font.Bold = True<o:p></o:p>
               Range("A1").Select<o:p></o:p>
<o:p></o:p>
               'Save, Mail, Close and Delete the file<o:p></o:p>
               Set OutMail = OutApp.CreateItem(0)<o:p></o:p>
<o:p></o:p>
               With NewWB<o:p></o:p>
               'The workbook will be saved with a name including the owner<o:p></o:p>
               'hence there can be no / or \ or other illegal characthers in the owner name<o:p></o:p>
                   .SaveAs TempFilePath & TempFileName _<o:p></o:p>
                         & FileExtStr, FileFormat:=FileFormatNum<o:p></o:p>
<o:p></o:p>
                   On Error Resume Next<o:p></o:p>
                   With OutMail<o:p></o:p>
                       .SentOnBehalfOfName = "me@mail.dk"<o:p></o:p>
                       .to = mailAddress<o:p></o:p>
                       .Subject = "Consultant Report"<o:p></o:p>
                       .Attachments.Add NewWB.FullName<o:p></o:p>
                       .Body = "Hello," & Chr(13) & Chr(13) _<o:p></o:p>
                       & "Attached you will find a report of your currently active contract(s)" & Chr(13) _<o:p></o:p>
                       & "Please send us your feedback if the attached information is incorrect or needs an update." & Chr(13) _<o:p></o:p>
                       & "Feedback and questions can be directed to me@mail.dk." & Chr(13) & Chr(13) _<o:p></o:p>
                       & "Best regards," & Chr(13) & "Group Services" & Chr(13)<o:p></o:p>
                       .Display<o:p></o:p>
                   End With<o:p></o:p>
                   On Error GoTo 0<o:p></o:p>
                   .Close savechanges:=False<o:p></o:p>
               End With<o:p></o:p>
<o:p></o:p>
               Set OutMail = Nothing<o:p></o:p>
               Kill TempFilePath & TempFileName & FileExtStr<o:p></o:p>
           End If<o:p></o:p>
<o:p></o:p>
           'Close AutoFilter<o:p></o:p>
           Ash.AutoFilterMode = False<o:p></o:p>
<o:p></o:p>
       Next Rnum<o:p></o:p>
   End If<o:p></o:p>
<o:p></o:p>
cleanup:<o:p></o:p>
   Set OutApp = Nothing<o:p></o:p>
   Application.DisplayAlerts = False<o:p></o:p>
   Cws.Delete<o:p></o:p>
   Application.DisplayAlerts = True<o:p></o:p>
<o:p></o:p>
   With Application<o:p></o:p>
       .EnableEvents = True<o:p></o:p>
       .ScreenUpdating = True<o:p></o:p>
   End With<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
I have a workbook with data and macro that works if anybody ever needs to test it out.<o:p></o:p>
PM me for the workbook (I have no rights to upload).<o:p></o:p>
<o:p></o:p>
B.R.<o:p></o:p>
Skjoldborg<o:p></o:p>

Hi ! This work very fine , how should I get all files to the same email




Thanks !
 
Upvote 0
Hi ! This work very fine , how should I get all files to the same email

Thanks !

I would think you have to mingle a bit round with the code.
I tried testing this, but I dont have Outlook on any of my PC's at the moment so the code fails to run on my pc.

I cant give you an answer at the moment
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,690
Members
449,329
Latest member
tommyarra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top