Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA代码用于查找范围中的值,并跨值返回单元格_Excel_Vba - Fatal编程技术网

Excel VBA代码用于查找范围中的值,并跨值返回单元格

Excel VBA代码用于查找范围中的值,并跨值返回单元格,excel,vba,Excel,Vba,我希望有人能帮我找出一行我无法理解的代码。VBA编码不是我的强项。此代码来自的工作簿将自动通过电子邮件发送M列中的地址。然后,它将从该行主题行和正文中使用的其他单元格中提取数据。我遇到的问题是行H列中的值。该单元格中的值是用户ID。该用户ID可以在用户工作表的范围(“A2:A18”)中找到。用户的电子邮件地址,这是我正在寻找的值,位于同一页的范围(“B2:B18”)。下面代码的摘录适用于“请将收尾文档通过电子邮件发送至:&Cells(cell.Row,“H”).Value” 非常感谢您的帮助 P

我希望有人能帮我找出一行我无法理解的代码。VBA编码不是我的强项。此代码来自的工作簿将自动通过电子邮件发送M列中的地址。然后,它将从该行主题行和正文中使用的其他单元格中提取数据。我遇到的问题是行H列中的值。该单元格中的值是用户ID。该用户ID可以在用户工作表的范围(“A2:A18”)中找到。用户的电子邮件地址,这是我正在寻找的值,位于同一页的范围(“B2:B18”)。下面代码的摘录适用于“请将收尾文档通过电子邮件发送至:&Cells(cell.Row,“H”).Value”

非常感谢您的帮助

Private Sub CommandButton1_Click()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("M").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
       LCase(Cells(cell.Row, "A").Value) = "yes" _
       And LCase(Cells(cell.Row, "A").Value) <> "Sent" Then

        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .To = cell.Value
            .Subject = Cells(cell.Row, "AD").Value
            .Body = "Dear " & Cells(cell.Row, "AC").Value & "," & vbNewLine & vbNewLine & strbody & _
            "Your closeout package for " & Cells(cell.Row, "C").Value & "/" & Cells(cell.Row, "D").Value & "/" & Cells(cell.Row, "E").Value & "/" & Cells(cell.Row, "F").Value & " is over 30 days past due." & vbNewLine & _
            "All closeout requirements are attached for your reference and due within 10 days of construction complete. Please email your closeout documents to: " & Cells(cell.Row, "H").Value & _
            "• Scheduled Construction Start Date - " & Cells(cell.Row, "X").Value & vbNewLine & _
            "• Construction Start Date - " & Cells(cell.Row, "V").Value & vbNewLine & _
            "• Construction Completed Date- " & Cells(cell.Row, "W").Value & vbNewLine & vbNewLine & _
            "• General Contractor - " & Cells(cell.Row, "N").Value & vbNewLine & _
            "• GC Name - " & Cells(cell.Row, "O").Value & vbNewLine & _
            "• GC Phone Number - " & Cells(cell.Row, "P").Value & vbNewLine & _
            "• GC Email - " & Cells(cell.Row, "Q").Value & vbNewLine & vbNewLine & _
            "• Company - " & Cells(cell.Row, "J").Value & vbNewLine & _
            "• Name - " & Cells(cell.Row, "K").Value & vbNewLine & _
            "• Phone Number - " & Cells(cell.Row, "L").Value & vbNewLine & _
            "• Email - " & Cells(cell.Row, "M").Value & vbNewLine & vbNewLine

            .Send
        End With
        On Error GoTo 0
        Cells(cell.Row, "A").Value = "Sent"
        Set OutMail = Nothing
    End If
Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True

End Sub
Private子命令按钮1\u单击()
Dim OutApp作为对象
将邮件变暗为对象
暗淡单元格作为范围
像弦一样暗的链子
Application.ScreenUpdating=False
Set-OutApp=CreateObject(“Outlook.Application”)
关于错误转到清理
对于列(“M”).Cells.SpecialCells(xlCellTypeConstants)中的每个单元格
如果单元格值像“*@*。?*”和_
LCase(单元格(cell.Row,“A”).Value)=“是”_
然后将LCase(Cells(cell.Row,“A”).Value)“发送”
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
发邮件
.To=单元格.Value
.Subject=单元格(cell.Row,“AD”)。值
.Body=“亲爱的”单元格(cell.Row,“AC”).Value&“,”&vbNewLine&vbNewLine&strobdy&_
“您的”&Cells(cell.Row,“C”)。Value&“/”&Cells(cell.Row,“D”)。Value&“/”&Cells(cell.Row,“E”)。Value&“Value&“/”&Cells(cell.Row,“F”)。Value&“逾期超过30天。”&vbNewLine&_
“附上所有收尾要求供您参考,并在施工完成后10天内到期。请将收尾文件通过电子邮件发送至:“&Cells(cell.Row,“H”).Value&_
“•计划施工开始日期-”&单元格(cell.Row,“X”).值和vbNewLine&_
“•施工开始日期-”&单元格(cell.Row,“V”).值和vbNewLine&_
“•施工完成日期-”&单元格(cell.Row,“W”)。数值和vbNewLine&vbNewLine&_
“•总承包商-”&单元格(cell.Row,“N”).值和vbNewLine&_
“•GC名称-”&单元格(cell.Row,“O”).值和vbNewLine&_
“•GC电话号码-”&单元格(cell.Row,“P”)。Value&vbNewLine&_
“•GC电子邮件-”&Cells(cell.Row,“Q”).Value&vbNewLine&vbNewLine&_
“•公司-”&单元格(cell.Row,“J”).价值与vbNewLine&_
“•名称-”&单元格(cell.Row,“K”).值和vbNewLine&_
“•电话号码-”&单元格(cell.Row,“L”)。Value&vbNewLine&_
“•电子邮件-”&Cells(cell.Row,“M”).Value&vbNewLine&vbNewLine
.发送
以
错误转到0
单元格(cell.Row,“A”).Value=“已发送”
发送邮件=无
如果结束
下一个细胞
清理:
设置应用程序=无
Application.ScreenUpdating=True
端接头

也许可以使用
Application.Vlookup
根据id查找电子邮件地址,如下所示:

Set OutMail = OutApp.CreateItem(0)

Dim emailTo As Variant
emailTo = Application.Vlookup(Cells(cell.Row, "H").Value, Sheets("Users").Range("A2:B18"), 2, False)

... "Please email your closeout documents to: " & emailTo ...

请注意,如果Vlookup失败,
Application.Vlookup
将返回一个错误值,因此您可以使用
iError(emailTo)测试结果

也许可以使用
Application.Vlookup
根据ID查找返回电子邮件地址。@BigBen我曾尝试使用Vlookup,但不断出现错误。@Grags-向我们展示你用Vlookup尝试了什么以及失败的地方(或方式),我们可以帮助解决问题。