过滤和电子邮件Excel文件(VBA)
我有一个帐户列表和相关信息,我必须将其拆分,并将特定帐户发送给某些人。这需要做大约50次。我已经有了一个程序设置,可以过滤、复制数据到新文件并保存。有没有办法设置它,然后根据联系人列表通过电子邮件发送此文件 每个帐户都由一个地区覆盖,因此我有一个列表,其中包含该地区和联系人的电子邮件。在按区域分割的宏中,它具有这些区域的数组,因此可以从联系人列表中进行某种查找吗 代码:过滤和电子邮件Excel文件(VBA),vba,excel,Vba,Excel,我有一个帐户列表和相关信息,我必须将其拆分,并将特定帐户发送给某些人。这需要做大约50次。我已经有了一个程序设置,可以过滤、复制数据到新文件并保存。有没有办法设置它,然后根据联系人列表通过电子邮件发送此文件 每个帐户都由一个地区覆盖,因此我有一个列表,其中包含该地区和联系人的电子邮件。在按区域分割的宏中,它具有这些区域的数组,因此可以从联系人列表中进行某种查找吗 代码: 我假设你想用VB编程,你可以这样做 Dim msg As System.Web.Mail.MailMessage = New
我假设你想用VB编程,你可以这样做
Dim msg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
msg.From = "noone@nobody.com"
msg.To = "someone@somewhere.com"
msg.Subject = "Email with Attachment Demo"
msg.Body = "This is the main body of the email"
Dim attch As MailAttachment = New MailAttachment("C:\attachment.xls")
msg.Attachments.Add(attch)
SmtpMail.Send(msg)
如果您遇到上述问题,“我的邮件”宏不同;这与excel 2007一起使用:
Sub Mail()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "To Whom It May Concern:" & vbNewLine & vbNewLine & _
"This is a test!" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.to = "anyone@anywhere.com"
.cc = ""
.BCC = ""
.Subject = "This is only a test"
.Body = strbody
'You can add an attachment like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
乔恩
我假设如下
1) 各地区都在山口啊
2) 联系人在Col AI中
3) 代码中的UniqueItems()是否删除重复项
请尝试下面的代码。我已经对代码进行了注释,因此请仔细阅读并进行相关更改。尤其是保存文件的部分。我使用了Outlook的后期绑定
注意:我总是在发布之前测试我的代码,但在当前的情况下,如果发现任何错误,我不能这样做,请告诉我
Option Explicit
Sub SplitFile()
'~~> Excel variables
Dim wb As Workbook, wbtemp As Workbook
Dim rTemp As Range, rng As Range
Dim regions() As String, FileExt As String, flName As String
Dim N As Long, FileFrmt As Long
'~~> OutLook Variables
Dim OutApp As Object, OutMail As Object
Dim strbody As String, strTo As String
On Error GoTo Whoa
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
'~~> Just Regions
Set rTemp = wb.Sheets("Combined").Range("AH2:AH1455")
'~~> Regions and Email address. We wil require this later
'~~> Tofind email addresses
Set rng = wb.Sheets("Combined").Range("AH2:AI1455")
regions = UniqueItems(rTemp, False)
'~~> Create an instance of outlook
Set OutApp = CreateObject("Outlook.Application")
For N = 1 To UBound(regions)
Set wb1 = Workbooks.Add
wb.Sheets("DVal").Copy after:=wb1.Sheets(1)
With wb.Sheets("Combined")
.AutoFilterMode = False
With .Range("A1:BP1455")
.AutoFilter Field:=34, Criteria1:=regions(N)
'~~> I think you want to copy the filtered data???
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy _
wb1.Sheets("Sheet1").Range("A1")
For c = 1 To 68
wb1.Sheets("Sheet1").Range("A1:BP2694").Columns(c).ColumnWidth = _
wb.Columns(c).ColumnWidth
Next c
End With
End With
'~~> Set the relevant Fileformat for Save As
' 51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
' 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
' 50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
' 56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)
FileFrmt = 52
Select Case FileFrmt
Case 50: FileExt = ".xlsb"
Case 51: FileExt = ".xlsx"
Case 52: FileExt = ".xlsm"
Case 56: FileExt = ".xls"
End Select
'~~> Contruct the file name.
flName = "H:\" & regions(N) & " 14-12-11" & FileExt
'~~> Do the save as
wb1.SaveAs Filename:=flName, FileFormat:=FileFrmt
wb1.Close SaveChanges:=False
'~~> Find the email address
strTo = Application.WorksheetFunction.VLookup(regions(N), rng, 2, 0)
'~~> Create new email item
Set OutMail = OutApp.CreateItem(0)
'~~> Create the body of the email here. Change as applicable
strbody = "Dear Mr xyz..."
With OutMail
.To = strTo
.Subject = regions(N) & " 14-12-11" '<~~ Change subject here
.Body = strbody
.Attachments.Add flName
'~~> Uncomment the below if you just want to display the email
'~~> and comment .Send
'.Display
.Send
End With
Next N
LetContinue:
Application.ScreenUpdating = True
'~~> CleanUp
On Error Resume Next
Set wb = Nothing
Set wb1 = Nothing
Set OutMail = Nothing
OutApp.Quit
Set OutApp = Nothing
On Error GoTo 0
Whoa:
MsgBox Err.Description
Resume LetContinue
End Sub
选项显式
子拆分文件()
'~~>Excel变量
将wb设置为工作簿,wbtemp设置为工作簿
变暗rTemp作为范围,rng作为范围
Dim regions()作为字符串,FileExt作为字符串,flName作为字符串
尺寸N等于长,文件FRMT等于长
“~~>OutLook变量
Dim OutApp作为对象,OutMail作为对象
Dim strbody作为字符串,strTo作为字符串
关于错误转到哇
Application.ScreenUpdating=False
设置wb=ActiveWorkbook
“~~~>只是区域
设置rTemp=wb.Sheets(“组合”).范围(“AH2:AH1455”)
“~~>地区和电子邮件地址。我们以后会要求的
“~~>以查找电子邮件地址
设置rng=wb.Sheets(“组合”).范围(“AH2:AI1455”)
区域=唯一项(rTemp,False)
“~~>创建outlook的实例
Set-OutApp=CreateObject(“Outlook.Application”)
对于N=1到UBound(区域)
设置wb1=工作簿。添加
wb.Sheets(“DVal”)。之后的副本:=wb1.Sheets(1)
带工作分解表(“组合”)
.AutoFilterMode=False
范围(“A1:BP1455”)
.自动筛选字段:=34,标准1:=区域(N)
“~~>我想你想复制过滤后的数据???
.偏移量(1,0).特殊单元格(xlCellTypeVisible).复制_
wb1.图纸(“图纸1”).范围(“A1”)
对于c=1到68
wb1.表(“表1”).范围(“A1:BP2694”).列(c).列宽=_
wb.列(c).列宽
下一个c
以
以
“~~>为另存为设置相关的文件格式
'51=xlOpenXMLWorkbook(2007-2010年无宏,xlsx)
'52=xlOpenXMLWorkbookMacroEnabled(在2007-2010年期间有或没有宏,xlsm)
'50=xlExcel12(2007-2010年Excel二进制工作簿,带或不带宏,xlsb)
'56=xlExcel8(Excel 2007-2010中的97-2003格式,xls)
FileFrmt=52
选择案例文件FRMT
案例50:FileExt=“.xlsb”
案例51:FileExt=“.xlsx”
案例52:FileExt=“.xlsm”
案例56:FileExt=“.xls”
结束选择
“~~>构造文件名。
flName=“H:\”&区域(N)&“14-12-11”&文件扩展
“~~>执行另存为操作
wb1.SaveAs文件名:=flName,文件格式:=FileFrmt
wb1.关闭保存更改:=False
“~~>查找电子邮件地址
strTo=Application.WorksheetFunction.VLookup(区域(N)、rng、2、0)
“~~>创建新的电子邮件项目
Set-OutMail=OutApp.CreateItem(0)
“~~>在此处创建电子邮件正文。更改(如适用)
strbody=“亲爱的xyz先生…”
发邮件
.To=strTo
.Subject=区域(N)&“14-12-11”和注释。发送
"展示,
.发送
以
下一个
让我们继续:
Application.ScreenUpdating=True
“~~>清理
出错时继续下一步
设置wb=Nothing
设置wb1=Nothing
发送邮件=无
OutApp,退出
设置应用程序=无
错误转到0
哇
MsgBox错误说明
继续
端接头
Awesome,你知道我如何根据联系人所在的地区从列表中查找联系人吗?你能发布联系人和地区列表的外观吗?另外,你对这两个变量的动态分配给我错误,我使用的是2007,这是为什么?联系人列表只是一列表示区域,一列相邻的列带有相应的联系人。
Option Explicit
Sub SplitFile()
'~~> Excel variables
Dim wb As Workbook, wbtemp As Workbook
Dim rTemp As Range, rng As Range
Dim regions() As String, FileExt As String, flName As String
Dim N As Long, FileFrmt As Long
'~~> OutLook Variables
Dim OutApp As Object, OutMail As Object
Dim strbody As String, strTo As String
On Error GoTo Whoa
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
'~~> Just Regions
Set rTemp = wb.Sheets("Combined").Range("AH2:AH1455")
'~~> Regions and Email address. We wil require this later
'~~> Tofind email addresses
Set rng = wb.Sheets("Combined").Range("AH2:AI1455")
regions = UniqueItems(rTemp, False)
'~~> Create an instance of outlook
Set OutApp = CreateObject("Outlook.Application")
For N = 1 To UBound(regions)
Set wb1 = Workbooks.Add
wb.Sheets("DVal").Copy after:=wb1.Sheets(1)
With wb.Sheets("Combined")
.AutoFilterMode = False
With .Range("A1:BP1455")
.AutoFilter Field:=34, Criteria1:=regions(N)
'~~> I think you want to copy the filtered data???
.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy _
wb1.Sheets("Sheet1").Range("A1")
For c = 1 To 68
wb1.Sheets("Sheet1").Range("A1:BP2694").Columns(c).ColumnWidth = _
wb.Columns(c).ColumnWidth
Next c
End With
End With
'~~> Set the relevant Fileformat for Save As
' 51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
' 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
' 50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
' 56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)
FileFrmt = 52
Select Case FileFrmt
Case 50: FileExt = ".xlsb"
Case 51: FileExt = ".xlsx"
Case 52: FileExt = ".xlsm"
Case 56: FileExt = ".xls"
End Select
'~~> Contruct the file name.
flName = "H:\" & regions(N) & " 14-12-11" & FileExt
'~~> Do the save as
wb1.SaveAs Filename:=flName, FileFormat:=FileFrmt
wb1.Close SaveChanges:=False
'~~> Find the email address
strTo = Application.WorksheetFunction.VLookup(regions(N), rng, 2, 0)
'~~> Create new email item
Set OutMail = OutApp.CreateItem(0)
'~~> Create the body of the email here. Change as applicable
strbody = "Dear Mr xyz..."
With OutMail
.To = strTo
.Subject = regions(N) & " 14-12-11" '<~~ Change subject here
.Body = strbody
.Attachments.Add flName
'~~> Uncomment the below if you just want to display the email
'~~> and comment .Send
'.Display
.Send
End With
Next N
LetContinue:
Application.ScreenUpdating = True
'~~> CleanUp
On Error Resume Next
Set wb = Nothing
Set wb1 = Nothing
Set OutMail = Nothing
OutApp.Quit
Set OutApp = Nothing
On Error GoTo 0
Whoa:
MsgBox Err.Description
Resume LetContinue
End Sub