通过VBA通过电子邮件发送后删除工作表

时间:2015-06-30 15:11:47

标签: excel vba excel-vba

我有一个宏将工作表拆分成几个工作表,然后通过电子邮件发送每个工作表并删除已拆分的初始工作簿。虽然有很多数据,它冻结了我的电脑。有没有办法可以在通过电子邮件发送后添加删除工作表? 我只是在最后拨打电话来调用其他宏。但是要处理太多数据。

Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
vcol = 13
'above defines your variable or laction or the the "unique" worksheet it will be named after _
for this particular macro is the start point
'Added 6.29.15 additional variable to make the "Sheet1" into a variable instead or renaming sheet constantly
Set ws = Sheets("Sheet1")
'worksheet name int he """" : )
'the rest is just Auto for filling in specifics

lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1:Z1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
    On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match _
      (ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
        ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
Next
myarr = Application.WorksheetFunction.Transpose _
        (ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
    ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
        Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
        Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
    ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets _
        (myarr(i) & "").Range("A1")
    Sheets(myarr(i) & "").Columns.AutoFit
Next
ws.AutoFilterMode = False
ws.Activate
End Sub
'MACRO will copy a dynamic range and both x and y axis

Sub sbDeleteASheet()

'Stopping Application Alerts
Application.DisplayAlerts = False

Sheets("Sheet1").Delete

'Enabling Application alerts once we are done with our task
Application.DisplayAlerts = True

End Sub

Sub RequestorReport6_4()
'Working in 2000-2010
    Dim Source As Range
    Dim Dest As Workbook
    Dim wb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim FileFormatNum As Long
   Dim OutApp As Object
    Dim OutMail As Object


For Each sht In ActiveWorkbook.Sheets
    sht.Activate
    SendTo = sht.Range("Z2").Value

    Set Source = Nothing
    On Error Resume Next
    Set Source = Range("A:Z").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, " & _
               "please correct and try again.", vbOKOnly
        Exit Sub
    End If
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set wb = ActiveWorkbook


    Set Dest = Workbooks.Add(xlWBATWorksheet)
    Source.Copy
    With Dest.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial Paste:=xlPasteValues
        .Cells(1).PasteSpecial Paste:=xlPasteFormats
        .Cells(1).Select
        Application.CutCopyMode = False
    End With
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "KIP Delivery Status Report" & Format(Now, "dd-mmm-yy h-mm-ss")
    If Val(Application.Version) < 12 Then
        'You use Excel 2000-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2010
        FileExtStr = ".xlsx": FileFormatNum = 51
    End If
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With Dest
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .SentOnBehalfOfName = "francisco.alvarez1@kiewit.com"
            .To = SendTo
            .CC = "francisco.alvarez1@kiewit.com"
            .BCC = ""
            .Subject = "KIP Daily Delivery Status Report"
             .body = "Hello," & Chr(10) & "Attached is your Field Delivery Report showing any items that you have on order." & Chr(10) & "Items that need to be received in MIGO are highlighted in either YELLOW or RED. Red items have an invoice blocked for payment due to the goods receipt and need to be received immeditatly. Yellow items are past their delivery date.  All other items are set to be delivered in the future. If you have not received any of the highlighted items identified on this report please forward this email with the PO# and items you are missing to KIP.Expediting@kiewit.com" & Chr(10) & "MIGO Instructions can be found here: http://goo.gl/N0yn1l" & Chr(10) & "The Delivery Status Dashbaord and trainings can be found here:  http://goo.gl/AealbM"
            .Attachments.Add Dest.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Send
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
    Kill TempFilePath & TempFileName & FileExtStr
    Set OutMail = Nothing
    Set OutApp = Nothing
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Next
End Sub

0 个答案:

没有答案