Send Email Data with Hidden rows without Values

chaboyski

Board Regular
Joined
Aug 18, 2020
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
Hi Team,

i want to copy the cells C22:J60, but, cells C40:J60 should be hidden if there is no data then send email.

i dont need it to be hidden on the file, i just wanted it to be hidden on the email.

How would i be able to include it on the code below?

VBA Code:
 Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim strbody2 As String
    Dim objWord
    Dim objDoc
    Dim objSelection
    Dim objShapes
    Dim i As Integer
    Dim strValue As String
    Dim ws As Worksheet
   
    Today = Format(Now(), "mm.dd.yyyy")
   
    'Attach Range to report
    Set rng = Nothing
    On Error Resume Next
        ' Only send the visible cells in the selection.
    Set rng = Sheets("Standard Outage Template").Range("C22:J60").SpecialCells(xlCellTypeVisible)
        ' You can also use a range with the following statement.
        ' Set rng = Selection.SpecialCells(xlCellTypeHidden)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected. " & _
               vbNewLine & "Please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'This is the email body

    On Error Resume Next
   
    With OutMail
        .Display
        .SentOnBehalfOfName = Sheets("Recipients").Range("C1")
        .To = Sheets("Recipients").Range("r2")
        .CC = Sheets("Recipients").Range("C3")
        .BCC = Sheets("Recipients").Range("C6")
        .Subject = Sheets("Recipients").Range("C10")
        .HTMLBody = strbody & RangetoHTML(rng) & strbody2 & .HTMLBody
        .Display ' display before sending or use .Send
    End With
   
    On Error GoTo 0
   
    With Application
        .EnableEvents = True
        .ScreenUpdating = False
    End With

    Set OutApp = Nothing
    Set OutMail = Nothing

'This code will hide the Formula sheet
 For Each ws In ActiveWorkbook.Worksheets
        If InStr(ws.Name, "Formula") > 0 Then
            ws.Visible = xlSheetHidden
        End If
    Next ws

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
"Sheets("Standard Outage Template").Range("C22:J60")."

Make the range dynamic by finding the last row, then

Excel Formula:
Sheets("Standard Outage Template").Range("C22:J" & LastRow).
 
Upvote 1
Solution

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top