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!
Bookmarks