Makro einmal pro Worksheet ausführen
12.03.2021 11:53:46
Ingo
ich habe mal wieder eine Schreibblockade.
In einem Workbook habe ich 36 verschidene Worksheets.
Jedes enthält einen Header, ein paar Daten und eine Mailadresse.
Jedes Worksheet soll jetzt an die jeweilige Maiadresse geschickt werden.
Dafür habe ich ein praktisches Makro. Leider kann ich das immer nur für ein Worksheet aktivieren.
Könnt ihr mir dabei helfen dieses Makro in einen Loop zu bringen, der jedes einzelne Worksheet als outlook mail verschickt?
Mein Makro sieht so aus:
Sub SendWorkSheet()
'The Monthly Areaplanner Overview per country
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim Dates As Variant
Dim Mail As Variant
Mail = Range("k2").Text
Dates = Range("B3").Text
On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = Mail
.CC = ""
.BCC = ""
.Subject = "Monthly Country AreaPlan Overview"
.Body = "Dear AreaPlanner Approver," & vbCrLf & _
" " & vbCrLf & _
"here you are with the 'AreaPlan Monthly Overview' of your country." & vbCrLf & _
"The Overview shows how many SVCs are covered by an actual AreaPlan by the date of " _
& Dates & "." & vbCrLf & _
"Thanks a lot." & vbCrLf & _
" " & vbCrLf & _
"With Kind Regards" & vbCrLf & _
"Your EU-NOEP Team"
.Attachments.Add Wb2.FullName
.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
Im Voraus vielen DAnkIngo