代码之家  ›  专栏  ›  技术社区  ›  Al Grant

使用Excel中电子邮件地址的筛选列创建新电子邮件

  •  0
  • Al Grant  · 技术社区  · 7 年前

    在Excel vba 2013中,希望使用Excel中筛选的单元格作为电子邮件地址的源,以传递给生成新电子邮件的过程。

    我的数据如下:

    enter image description here

    我有一行代码,应该在标题行后的B列中选择所有数据(电子邮件地址):

    Dim recipients As String
    ....
    recipients = Join(Application.Transpose(.Range("B6" & .Rows.Count).Value), ";")
    

    此行产生一个无效或不合格引用的错误。

    收件人字符串稍后传递到电子邮件过程。为完整起见,电子邮件vba是:

    With OutMail
      .Subject = "Some subject"
      .Body = "Get a body for the email"
      .To = recipients
      .Importance = olImportanceHigh
      .Display
    End With
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Edward    7 年前

    下面是如何在代码中迭代筛选的单元格:

    Dim recipients As String
    Dim recipient As Range
    With ThisWorkbook.Sheets("Sheet1") ' Or ActiveSheet
       For Each recipient In .Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Cells
          recipients = recipients + recipient + ";"
       Next recipient
    End With
    
        2
  •  0
  •   Edward    7 年前

    1:您的范围必须是: Range("B6:B" & .Rows.Count)

    2:您应该将行限制在使用区域 Range("B6:B" & .UsedRange.Rows.Count)

    3:只选择需要添加的筛选单元格 .SpecialCells(xlCellTypeVisible)

    像这样:

    recipients = Join(Application.Transpose(.Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Value), ";")
    

    4:最好是在代码中迭代单元格,而不是使用转置/联接:否则,如果只筛选了一个电子邮件地址,或者看不到任何电子邮件,则可能会出现错误。

    推荐文章