如果列A为空,则从行添加值,直到列A VBA中的新值

时间:2018-04-10 20:43:46

标签: vba excel-vba excel

我创建了一系列程序,根据点击按钮所在行中的值创建预先写好的电子邮件(只有在A列中有值时才会插入按钮)。我想要做的还包括列F和H中的值,当A列中的单元格为空时,直到到达A列中的下一个单元格并带有值(即放置单元格F6和H6的值以及F7和H7在电子邮件字符串中但因为A8中有值而停止

到目前为止,这是我的代码:

     Sub SendEmail()
     Dim Outlook_App As Object
     Dim Outlook_Mail As Object
     Dim r As Range

       Set Outlook_App = CreateObject("Outlook.Application")
       Set Outlook_Mail = Outlook_App.CreateItem(0)
       Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
       Dim strbody As String

       strbody = "Hello," & vbNewLine & vbNewLine & _
       "Our records indicate we need to receive the following items from " & 
       Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Offset(0, 
       -11).Value & ":" & vbNewLine & vbNewLine & vbNewLine & _
       "Thank you,"

        On Error Resume Next
        With Outlook_Mail
       .To = Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Offset(0, 
       -4).Value
       .Subject = "ACTION NEEDED: Request for Financial Items"
       .body = strbody
       .display
       End With
       End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

我没有测试过代码:这可能对你有所帮助。使用IF THEN ELSE语句,代码略有不同strbody。第一个IF检查r.Row 中第一个单元格下方的单元格是否为空,并使用您当前的strbody代码。如果ELSE 中第一个单元格下方的单元格为空,则r.Row语句将运行;代码Cells(r.Row, r.Column).Offset(1, -8).Value & Cells(r.Row, r.Column).Offset(1, -6).Value将列F和H中单元格的值添加一行。

If Cells(r.Row, r.Column).Offset(1, -13).Value <> "" Then

    strbody = "Hello," & vbNewLine & vbNewLine & _
    "Our records indicate we need to receive the following items from " & _
    Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Offset(0, -11).Value & _
    ":" & vbNewLine & vbNewLine & vbNewLine & "Thank you,"

Else
    strbody = "Hello," & vbNewLine & vbNewLine & _
    "Our records indicate we need to receive the following items from " & _
    Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Offset(0, -11).Value & _
    Cells(r.Row, r.Column).Offset(1, -8).Value & Cells(r.Row, r.Column).Offset(1, -6).Value & _
    ":" & vbNewLine & vbNewLine & vbNewLine & "Thank you,"

End If