Excel / VBA迭代列中的单元格从单元格获取电子邮件,验证并发送自动通知

时间:2017-08-14 07:24:18

标签: excel vba excel-vba

我想创建一个Excel文件,人们可以在其中输入他们的电子邮件地址,只要有5个人添加了他们的电子邮件,就会向所有这些电子邮件发送消息。

要在保存时执行VBA-Makro,请执行以下操作:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'My Code is here
End Sub

我尝试循环一列,但没有找到一个好的解决方案 - 目前我已经设置了从B2到B99的Rage 我需要从单元格中获取电子邮件,忽略空/空单元格,我需要以某种方式检查电子邮件是否有效(RegEx?)

Dim eMailList As String
Dim eMailCount As Integer

eMailCount = 0
eMailList = ""

Dim eMail As String

Dim rng As Range: Set rng = ThisWorkbook.Worksheets("Tabelle1").Range("B2:B99")
Dim i As Integer
For i = 1 To rng.Rows.Count
    eMail = rng.Cells(RowIndex:=i, ColumnIndex:="B").Text
eMailList = eMailList & eMail & "; "
Next


Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)
    With objMail
        .To = eMailList
        .Subject = "DummyText"
        .Body = "DummyText"
        .send
    End With

我的问题是:

  1. 如何轻松迭代一个特定列中的单元格?

  2. 如何从单元格中获取电子邮件 - .Text .Value?

  3. 如何在VBA / Excel中使用正则表达式来验证电子邮件?

  4. 如何列出电子邮件并向每封电子邮件发送讯息

  5. 我很高兴任何帮助/提示/推荐 在此先感谢

1 个答案:

答案 0 :(得分:0)

下面的代码将帮助您入门,代码注释中的解释:

Dim Rng As Range, C As Range

Set Rng = ThisWorkbook.Worksheets("Tabelle1").Range("B2:B99")
' loop through all cells in Range (Column "B")
For Each C In Rng
    If Trim(C.Value2) <> "" Then ' <-- check that cell isn't empty
        ' --- Add Here another IF criteria to validate your E-Mail ---

        eMail = Rng.Value2 ' <-- read the value2 of the cell (without the cell's format)
        eMailList = eMailList & eMail & "; "
    End If
Next C