【问题标题】:Using filtered column of email addresses in Excel to create new email在 Excel 中使用过滤后的电子邮件地址列创建新电子邮件
【发布时间】:2019-07-05 08:02:41
【问题描述】:

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

我的数据如下所示:

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

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

此行产生错误 Invalid or Unqualified Reference。

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

With OutMail
  .Subject = "Some subject"
  .Body = "Get a body for the email"
  .To = recipients
  .Importance = olImportanceHigh
  .Display
End With

【问题讨论】:

  • 您可能在您的.... 中某处缺少With

标签: excel vba


【解决方案1】:

以下是在代码中迭代过滤单元格的方法:

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】:

    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:在代码中迭代单元格而不是使用 Transpose/Join 可能会更好:否则,当仅过滤 1 个电子邮件地址或没有电子邮件可见时,您可能会收到错误。

    【讨论】:

    • 感谢有关迭代的提示。您有链接或示例吗?
    • 我添加了一个替代答案,显示如何在代码中迭代过滤的单元格,因为无法格式化 cmets。您可以根据需要进行调整,
    猜你喜欢
    • 2015-06-14
    • 2016-07-21
    • 1970-01-01
    • 2011-06-04
    • 2017-07-23
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多