简体   繁体   中英

Returning adjacent cell value in email subject & body

I'm trying to put together a piece of code but it keeps returning an error...have put together some very simple code in the past but am not so well versed in fixing code, and can't seem to troubleshoot what's going wrong in the code I have so far...

Background: I have an excel workbook for compliance (macro-enabled of course) that has various columns with due dates. If any of the columns have due dates within the next 30 days, column V will return "Y". At the start of the month, if any of the rows have "Y" listed in column V, an email alert will be sent to the specified email addresses with the name of the client (in column W) in both the subject and body.

Started off with identifying the cell reference of every "Y" occurrence (as "theValue"). The code seems to work without entering any values for the client name in the subject & body.

Have tried to use the offset value option but it keeps returning an error - am not sure how to rectify.

Any input is appreciated as to what I'm doing wrong - let me know if I can clarify on anything further in the meantime!

Sub notify()
    Dim Rng As Range
    For Each Rng In Range("V3:W200")
       If (Rng.Value = "Y") Then
        Call mymacro(Rng.Value)
       End If
    Next Rng
End Sub

Private Sub mymacro(theValue As String)
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hi All" & vbNewLine & vbNewLine & _
              "This alert has been automatically created by the Client Compliance Register. Please ensure information for " & Rng.Offset(0, 1).Value & " is up to date." & vbNewLine & vbNewLine

    With xOutMail
        .To = "generalmanager@gmail.com"
        .CC = "accountmanager@gmail.com"
        .BCC = "managingdirector@gmail.com"
        .Subject = "Detail(s) for " & Rng.Offset(0, 1).Value & " are expiring soon."
        .Body = xMailBody
        .Display   'use .Send once complete
    End With

    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing

End Sub

As alluded to in the comments:

rng only has procedure-level scope . The variable rng is only visible to the procedure notify . You can't use it inside mymacro .


To accomplish your task, pass rng.Offset(0, 1).Value as an argument when calling mymacro .

Call mymacro(Rng.Offset(0, 1).Value) 

The Call here is redundant; all you need is

mymacro Rng.Offset(0, 1).Value '<< no parentheses!

or

mymacro theValue:=Rng.Offset(0, 1).Value

Then within mymacro , change each instance of

Rng.Offset(0, 1).Value

to

theValue

because theValue is in scope for mymacro ; it's the argument you're passing.


For further reading, see understanding scope and visibility .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM