2014-01-12 185 views
2

我有一个包含7个工作表的工作簿。我在下面的vba中发送一封电子邮件,只要在特定的表格上满足某个值。从Excel发送多封电子邮件

每张纸都有不同的值和不同的附件发送。如何为每张表添加代码以便发送电子邮件?

在此先感谢

设置为一般(声明)

Option Explicit 

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Cells.Count > 1 Then Exit Sub 
    If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then 
     If IsNumeric(Target.Value) And Target.Value < 3500 Then 
      Call Fuel_LevelW03 
     End If 
    End If 
End Sub 

后跟一个模块 一般Fuel_LevelW03

Sub Fuel_LevelW03() 
    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim strbody As String 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    strbody = "Hi" & vbNewLine & vbNewLine & _ 
       "Please order fuel as attached." & vbNewLine & _ 
       "" & vbNewLine & _ 
       "Kind Regards" & vbNewLine & _ 
       "" 

    On Error Resume Next 
    With OutMail 
     .To = "email address" 
     .CC = "email address" 
     .BCC = "" 
     .Subject = "Fuel Order W03" 
     .Body = strbody 
     .Attachments.Add ("H:\Fuel Order Sheets\Glen Eden W03 Pump Station.xlsx") 
     .Send 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 
End Sub 
+0

_Change事件是针对每个工作表的,因此您需要在该级别编码陷阱。您可以概括该模块以接受来自各种工作表级调用的参数。 –

+0

谢谢安迪。所以,我要为每个工作表创建一个更改事件,以更改目标值和调用“名称”。我如何概括模块? – Trace

回答

0

据我所知,你尝试“讲方法“关于Target.Value是什么。就在参数传递到像这样的功能:

If IsNumeric(Target.Value) Then 
    If Target.Value < 3500 Then 
     Call Fuel_LevelW03(Sh.Name, Target.Value) 
    End If 
End If 

,并与这一个改变函数的名称:

Fuel_LevelW03(sheetName as String, targetValue as String) 
                'Change String to appropriate type 

EDIT2:我改变了代码张望了一下,如果你需要任何帮助让我知道。

编辑:好的,这是你如何解决这个问题。里面的“的ThisWorkbook”代码对象(下面的表代码对象,在代码编辑器的左侧),粘贴:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 
    If Target.Cells.Count > 1 Then Exit Sub 
    If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then 
     If IsNumeric(Target.Value) And Target.Value < 3500 Then 
      Call Fuel_LevelW03(Sh.Name) 
     End If 
    End If 
End Sub 

Sub Fuel_LevelW03(sheetName as String) 
    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim strbody As String 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 


    On Error Resume Next 

    If sheetName = "Sheet1" Then 'Replace Sheet1 with the name of your worksheet 

     strbody = "Hi" & vbNewLine & vbNewLine & _ 
       "Please order fuel as attached." & vbNewLine & _ 
       "" & vbNewLine & _ 
       "Kind Regards" & vbNewLine & _ 
       "STRING BODY1" 

     With OutMail 
      .To = "email address" 
      .CC = "email address" 
      .BCC = "" 
      .Subject = "Fuel Order W03" 
      .Body = strbody 
      .Attachments.Add ("H:\Fuel Order Sheets\Glen Eden W03 Pump Station.xlsx") 
      .Send 
     End With 
     On Error GoTo 0 

    ElseIf sheetName = "Sheet2" Then 'Replace Sheet2 with the name of the next sheet and 

     'Put the same content as the first IF statement, but adapted to "Sheet2" 

    ElseIf sheetName = "Sheet3" Then 'Replace Sheet3 with the name of the next sheet and 

     'Put the same content as the first IF statement, but adapted to "Sheet3" 

    ElseIf sheetName = "Sheet4" Then 'Replace Sheet4 with the name of the next sheet and 

     'Put the same content as the first IF statement, but adapted to "Sheet4" 

    'ElseIf ............. (So on, so forth) 


    End If 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

End Sub 

,只要你想你可以添加尽可能多的ElseIf的(每片)


我很确定这是你所需要的,虽然我不确定。

If ActiveSheet.Name = "Sheet1" Then 

    'Do something specific to "Sheet1" 

ElseIf ActiveSheet.Name = "Sheet2" Then 

    'Do something specific to "Sheet2" 

    'And so on so forth... 

End If 

你必须在每片一个按钮,宏观,并根据工作表上调用宏,你要发送一个不同的电子邮件,对不对?然后这将做到这一点。您可以根据需要添加尽可能多的ElseIf

+0

谢谢,我有点不确定,因为我不是这方面的专家。每个工作表的目标值是不同的数字,并且需要发送的附件也不同。我没有一个按钮来运行宏,只要值低于目标值,它就会自动发送电子邮件。此代码完美适用于一张工作表,我只需要将其复制到其他工作表,修改目标值和附件。在编码方面,我是一个新手。 – Trace

+0

是否可以在“目标值”中添加工作表名称? – Trace