+ Reply to Thread
Results 1 to 10 of 10

Macro to activate message if cell D1 is zero

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Macro to activate message if cell D1 is zero

    I have a macro to email a sheet with a message , which works fine



    However, should cell D1 be zero, then sheet must not be attached, an message in body of email to state

    "strBody = "Hi " & Sheets("BR1").Range("S1") "& please check your debtors ageing and hand over your problematic debtors for collection" & vbNewLine & vbNewLine &
     Sub Email_report()
    
    Dim File As String, strBody As String
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    File = Environ$("temp") & "\" & Format(Range("Q2"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"
    strBody = "Hi " & Sheets("BR1").Range("S1") & vbNewLine & vbNewLine & _
              "Attached, please find " & Sheets("BR1").Range("A1") & vbNewLine & vbNewLine & _
                "Please check attend to your slow paying debtors" & vbNewLine & vbNewLine & _
                 "Regards" & vbNewLine & vbNewLine & _
                "Howard"
    ActiveWorkbook.Save
    
    Sheets("BR1").Copy
    With ActiveWorkbook
       .SaveAs Filename:=File, FileFormat:=51
       .Close savechanges:=False
    End With
    With CreateObject("Outlook.Application").CreateItem(0)
        .Display
        .to = Join(Application.Transpose(Sheets("BR1").Range("R2:R5").Value), ";")
        .Subject = Sheets("Br1").Range("A1")
        .body = strBody
        .Attachments.Add File
        '.Send
    End With
    Kill File
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub



    I have attached sample data

    Your assistance in this regard is most appreciated
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro to activate message if cell D1 is zero

    You can add an if statement

    Sub Email_report()
    
    Dim File As String, strBody As String
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    File = Environ$("temp") & "\" & Format(Range("Q2"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"
    strBody = "Hi " & Sheets("BR1").Range("S1") & vbNewLine & vbNewLine & _
              "Attached, please find " & Sheets("BR1").Range("A1") & vbNewLine & vbNewLine & _
                "Please check attend to your slow paying debtors" & vbNewLine & vbNewLine & _
                 "Regards" & vbNewLine & vbNewLine & _
                "Howard"
    ActiveWorkbook.Save
    
    Sheets("BR1").Copy
    With ActiveWorkbook
       .SaveAs Filename:=File, FileFormat:=51
       .Close savechanges:=False
    End With
    With CreateObject("Outlook.Application").CreateItem(0)
        .Display
        .to = Join(Application.Transpose(Sheets("BR1").Range("R2:R5").Value), ";")
        .Subject = Sheets("Br1").Range("A1")
        .body = strBody
        If Range("D1").Value <> 0 Then
        .Attachments.Add File
        End If
        '.Send
    End With
    Kill File
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to activate message if cell D1 is zero

    thanks for the help Dave. If the value in D1 is zero, how do I amend message so that only the message below displays in the body ?
    If <> 0 then current message to display in body

  4. #4
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to activate message if cell D1 is zero

    Hi Dave

    I tried to amend the code so that if D1 = zero, then the applicable sheet is not to be attached and strbody1 message to appear in body

    When running the macro Email BR1 where D1 = 0 , the applicable sheet is attached and strbody1 does not appear in the body-strbody message appears



    Kindly check & amend my code


    Your assistance is most appreciated





     Sub Email_BR2()
    
    
    Dim File As String, strBody As String, strBody1 As String
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    File = Environ$("temp") & "\" & Format(Range("Q2"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"
    strBody = "Hi " & Sheets("BR2").Range("S1") & vbNewLine & vbNewLine & _
              "Attached, please find " & Sheets("BR2").Range("A1") & vbNewLine & vbNewLine & _
                "Please check attend to your slow paying debtors" & vbNewLine & vbNewLine & _
                 "Regards" & vbNewLine & vbNewLine & _
                "Howard"
          strBody1 = "Hi " & Sheets("BR2").Range("S2") & vbNewLine & vbNewLine & _
               "Attached, please find " & Sheets("BR2").Range("A1") & vbNewLine & vbNewLine & _
                "Please check your probematic and transfer to suspect debtors and hand these over if neccesary" & vbNewLine & vbNewLine & _
                 "Regards" & vbNewLine & vbNewLine & _
                "Howard"
                              
    ActiveWorkbook.Save
    
    Sheets("BR2").Copy
    With ActiveWorkbook
       .SaveAs Filename:=File, FileFormat:=51
       .Close savechanges:=False
    End With
    With CreateObject("Outlook.Application").CreateItem(0)
        .Display
        .to = Join(Application.Transpose(Sheets("BR2").Range("R2:R5").Value), ";")
        .Subject = Sheets("BR2").Range("A1")
        
        .to = Sheets("BR2").Range("R2:R2").Value
         If Range("D1").Value <> 0 Then
        .body = strBody
        Else
         .body = strBody1
         End If
          If Range("D1").Value <> 0 Then
        .Attachments.Add File
        End If
        '.Send
        End With
         
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,327

    Re: Macro to activate message if cell D1 is zero

    Are you sure you are on the correct sheet, because the code in itself works just fine.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to activate message if cell D1 is zero

    Hi Bakerman2

    Thanks for the reply.
    Sub Email_BR1() works fine as the value in D1 on sheet("Br1") <> 0

    however, Sub Email_BR2() , pertaining to sheet("Br2"), where the value in D1 = 0, there should be no attachment and the message in the body should pertaining to strbody1 and NOT STRbody

    Kind test and amend code

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,327

    Re: Macro to activate message if cell D1 is zero

    Specify the sheet for range D1 so Sheets("BR2").Range("D1").value.

  8. #8
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to activate message if cell D1 is zero

    Thanks, I realised that on checking my code

  9. #9
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,873

    Re: Macro to activate message if cell D1 is zero

    I see where my problem is


    I need to refer to the sheet on my macro, otherwise If I am not on the activate sheet macro does not work correctly

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,327

    Re: Macro to activate message if cell D1 is zero

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to activate message box to continue the macro.
    By markDuffy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-08-2015, 02:03 PM
  2. [SOLVED] Conditional Format for blank cell to activate error message in following cell
    By tittbb in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-28-2014, 04:07 AM
  3. [SOLVED] Activate vbCancel in message box
    By datafiend in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-25-2012, 04:41 AM
  4. Double Click to Activate Message Box
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2009, 01:29 AM
  5. Replies: 5
    Last Post: 06-05-2007, 10:06 AM
  6. [SOLVED] activate a cell value through a macro
    By TUNGANA KURMA RAJU in forum Excel General
    Replies: 1
    Last Post: 11-10-2005, 04:55 AM
  7. Cell Activate a Macro
    By icetrey in forum Excel - New Users/Basics
    Replies: 15
    Last Post: 03-17-2005, 06:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1