gpt4 book ai didi

excel - 如何将范围从 Excel 复制到 Outlook,忽略适用过滤器的第一列?

转载 作者:行者123 更新时间:2023-12-02 22:35:59 25 4
gpt4 key购买 nike

您好,我有一个代码可以过滤 A 列中的唯一值并从 A1:H 复制整个范围,但我想忽略第一列并希望从 B1:H 复制范围。

例如:如果有一个包含学生分数的表格,我想将个人分数表分别发布给每个学生。该宏发送表格以及第一列中的学生姓名,但我只需要标记表,不需要学生姓名。

这是我的代码

Sub Send_Row_Or_Rows_1()

Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim mailAddress As String
Dim StrBody As String

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

With Application
.EnableEvents = False
.ScreenUpdating = False
End With


Set Ash = ActiveSheet

'Set filter range and filter column (Column with names)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount

'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value

'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Mailinfo").Range("A1:B" & _
Worksheets("Mailinfo").Rows.Count), 2, False)
On Error GoTo 0

If mailAddress <> "" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With

Set OutMail = OutApp.CreateItem(0)

On Error Resume Next

With OutMail
.to = mailAddress
.Subject = "Test mail"
.HTMLBody = StrBody & RangetoHTML(rng)
.Display 'Or use Send

StrBody = Sheets("Body").Range("A1").Value & "<br>" & _
Sheets("Body").Range("A2").Value & "<br>" & _
Sheets("Body").Range("A3").Value & "<br><br><br>"

End With
On Error GoTo 0

Set OutMail = Nothing
End If

'Close AutoFilter
Ash.AutoFilterMode = False

Next Rnum
End If

cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

最佳答案

如果您想坚持使用您的解决方案而不是使用 Word's Mailing tools ,

只需更改此行:

Set rng = .SpecialCells(xlCellTypeVisible)

Set rng = Application.Intersect(.SpecialCells(xlCellTypeVisible),Ash.Range("B:H"))

关于excel - 如何将范围从 Excel 复制到 Outlook,忽略适用过滤器的第一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42807041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com