动态确定的单元格的返回值 [英] Return value of dynamically determined cell

查看:33
本文介绍了动态确定的单元格的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码验证"Sheet1"中的A列是否与"Sheet2"中的A列具有相同的值.如果是这样,应该从B列"Sheet2"中获取一个电子邮件地址.

The code below verifies if column A in "Sheet1" has the same value as column A in "Sheet2". If so, an email address should be taken from column B "Sheet2".

我的问题是从"Sheet2"列B中获取电子邮件地址.

My problem is getting the email address from "Sheet2" column B.

Sub mail()

    Dim A As Long
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim wb As Workbook
    Dim check

    Set wb = Excel.ActiveWorkbook
    Set sh1 = wb.Worksheets(1)
    Set sh2 = wb.Worksheets(2)
    Application.ScreenUpdating = False

    For A = 2 To sh1.Cells(Rows.Count, "A").End(xlUp).Row

        check = Application.match(sh1.Cells(A, 1).Value, sh2.Columns(1), 0)

        If IsError(check) Then
            MsgBox "No email was found!"
        Else
            ' i am not able to set this.
            'h = take the email address from sheet2 column B

            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.createItem(olmailitem)
            Set wb2 = ActiveWorkbook
            ActiveWorkbook.Save

            With OutMail
                .Display
                .To = h ' attached the email address
                .cc = ""
                .BCC = ""
                .Subject = "Test - " '& B & " " & F
                .htmlbody = "<p style='font-family:calibri;font-size:15'>" & "Hi " & C & "<BR/>" & "<BR/>" & "Please check the attached template." & "<br/>" & "<BR/>" & "Change data if required." & "<BR/>" & "<br/>" & "This e-mail has been automatically send! " & "<br/>" & "<br/>" & "With best regards," & "<br/>" & "<br/>" 
                .attachments.Add wb2.FullName
            End With

            ActiveWorkbook.Close              
        End If

    Next

End Sub

推荐答案

借助"PEH",我成功找到了解决方案:

With the help of "PEH" i succeed in finding a solution for this:

Sub mail()

    Dim A As Long
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim wb As Workbook

    Dim check

    Set wb = Excel.ActiveWorkbook
    Set sh1 = wb.Worksheets(1)
    Set sh2 = wb.Worksheets(2)

    For A = 2 To sh1.Cells(Rows.Count, "A").End(xlUp).Row
        check = Application.match(sh1.Cells(A, 1).Value, sh2.Columns(1), 0)

        If IsError(check) And Not IsEmpty(sh1.Cells(A, 1)) Then
            MsgBox "No email was found!"
        Else
            h = sh2.Cells(check, 2).Value


            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.createItem(olmailitem)
            Set wb2 = ActiveWorkbook
            wb.Save

            With OutMail
                .Display
                .To = h
                .cc = ""
                .BCC = ""
                .Subject = "Test - " '& B & " " & F
                .htmlbody = "<p style='font-family:calibri;font-size:15'>" & "Hi " & C & "<BR/>" & "<BR/>" & "Please check the attached template." & "<br/>" & "<BR/>" & "Change data if required." & "<BR/>" & "<br/>" & "This e-mail has been automatically send! " & "<br/>" & "<br/>" & "With best regards," & "<br/>" & "<br/>"
                .attachments.Add wb2.FullName
            End With

            wb.Close
        End If
Next

End Sub

这篇关于动态确定的单元格的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆