用于检查特定范围的单元格是否包含值的宏

时间:2011-08-21 16:34:37

标签: excel vba excel-vba

我正在尝试在excel中构建一个用于发送电子邮件的vb脚本宏。如果用户将列“F”中的值输入为“否”,则脚本检查单元L2是否包含值“0”。如果它包含“1”或任何其他值,则不会提示发送电子邮件。 该脚本完美无缺,除了我无法定义范围L2:L200。

这就是我想要做的: 即,如果用户在“F”列的任何一行中输入“NO”,并且如果“L”列中的同一行包含值为“0”,则应提示发送电子邮件。否则,它不应该。 请一些人帮助我,因为我对excel vb宏的了解有限。 I have left a space between the code where the range is defined

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                    ByVal lpszFile As String, ByVal lpszParams As String, _
                    ByVal LpszDir As String, ByVal FsShowCmd As Long) _
                    As Long
-------------------------------------------------------------------------------------------------------------------------
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim lngResponse As Long
Dim Mail_Object, Email_Subject, Email_Send_To, Email_Cc, Email_Bcc, Email_Body, Email_Body1, Email_Body2, Email_Body3 As String
  If Left(Target.Address, 2) = "$F" Then
  If Target.Value = "No" Then 


If Range("L2").Value = "0" Then


  lngResponse = MsgBox("Draft and send an email now ?", vbYesNo)
  If lngResponse = vbYes Then
Email_Subject = "Approval needed to process change order for PO " & Range("$C" & Right(Target.Address, 2)).Value & ""
    Email_Send_To = ""
    Email_Cc = ""
    Email_Bcc = ""
    Email_Body = "Hi ,"
    Email_Body1 = "   Please approve to process change order for PO# " & Range("$A" & Right(Target.Address, 2)).Value & ": "
    Email_Body2 = "   Actual price on the PO: $" & Range("$B" & Right(Target.Address, 2)).Value & ""
    Email_Body3 = " , Vendor quoted price: $" & Range("$C" & Right(Target.Address, 2)).Value & "" & "     " & "     " & "     " & "     " & "     " & "     " & "     " & "     "
    Mail_Object = "mailto:" & Email_Send_To & "?subject=" & Email_Subject & "&body=" & Email_Body & Email_Body1 & Email_Body2 & Email_Body3 & "&cc=" & Email_Cc '& "&bcc=" & Email_Bcc
    'On Error GoTo debugs
    ShellExecute 0&, vbNullString, Mail_Object, vbNullString, vbNullString, vbNormalFocus 
  End If
  End If
  End If
  End If
End Sub

1 个答案:

答案 0 :(得分:0)

对于提示发送电子邮件的任何行,固定单元格“L2”是否需要为0?或者您的意思是“如果更改的行中的L列为0,则仅提示发送电子邮件”?如果是这样,这是代码:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    'If column is F and entry is "no", case insensitive
    If Target.Column = 6 And UCase(Target.Value) = "NO" Then
        'If the L column on the changed cell's row equals 0
        If Sh.Cells(Target.Row, 12) = 0 Then
            'Prompt for email code here
        End If
    End If
End Sub

编辑:错过了结束