Cannot supress Display alert Message

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below

However when running the code, I get a message :"the following features cannot be saved in macro-free workbooks....

I want code to automatically select "Yes" and not to see this message

Also if Col H2 onwards is blank on sheet Macro , then macro to exit (I have formulas that result in a blank based on certain criteria)

When running the macro sheet1 is being created, which should not happen if if H2 onwards are blank

Kindly mend my code



Code:
Sub Email_Sheets()
    Dim ws As Worksheet
    Dim sFile As String, strBody As String, sName As String, strTo As String
    Dim filteredRange As Range, rng As Range
    Dim wsArr()
    Dim lr As Long, n As Long, i As Long
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    Set ws = Sheets("Macro")
    lr = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row
   
    For i = 2 To lr
        If ws.Range("J" & i).EntireRow.Hidden = False And ws.Range("J" & i).Value <> "" Then
            sName = ws.Range("J" & i).Value
            strTo = strTo & ws.Range("I" & i).Value & ";"
            ReDim Preserve wsArr(n)
            wsArr(n) = sName
            n = n + 1
        End If
    Next
   
    If n > 1 Then sName = "Guys"
    strBody = "Hi " & sName & vbNewLine & vbNewLine & _
        "Attached, please find Variance Reports pertaining to your branch" & vbNewLine & vbNewLine & _
        "Please attend to the variances and advise once corrected" & vbNewLine & vbNewLine & _
        "Regards" & vbNewLine & vbNewLine & _
        "Howard"
   
    sFile = ThisWorkbook.Path & "\" & "Stats Variances.xlsx"
   
    Dim newWB As Workbook ' Create a new workbook to copy the sheets
    Set newWB = Workbooks.Add
   
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim wsMacro As Worksheet
    Set wsMacro = wb.Sheets("Macro")
   
    ' Check if column H is empty or not visible
    Dim lastRowH As Long
    lastRowH = wsMacro.Cells(wsMacro.Rows.Count, "H").End(xlUp).Row
   
    If lastRowH < 2 Or wsMacro.Columns("H").Hidden Then
        MsgBox "No sheets to attach.", vbInformation
        Exit Sub
    End If
   
    ' Check if column H has any valid sheet names
    Dim hasValidSheet As Boolean
    hasValidSheet = False
   
    For i = 2 To lastRowH
        sName = wsMacro.Range("H" & i).Value
        If sName <> "" And WorksheetExists(CStr(sName), wb) Then
            hasValidSheet = True
            Exit For
        End If
    Next i
   
    If Not hasValidSheet Then
        MsgBox "No sheets to attach.", vbInformation
        Exit Sub
    End If
   
    For i = 2 To lastRowH
        sName = wsMacro.Range("H" & i).Value
        If WorksheetExists(CStr(sName), wb) Then
            wb.Sheets(CStr(sName)).Copy After:=newWB.Sheets(newWB.Sheets.Count)
        End If
    Next i
   
    On Error Resume Next ' Ignore errors if "Sheet1" doesn't exist
    Application.DisplayAlerts = False
    newWB.Sheets("Sheet1").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
   
    With newWB
        .SaveAs Filename:=sFile, FileFormat:=51
        .Close savechanges:=False
    End With
   
    With CreateObject("Outlook.Application").CreateItem(0)
        .Display
        .To = strTo
        .Subject = "Variance Report"
        .Body = strBody
       
        .Attachments.Add sFile
    End With
   
    Kill sFile
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Function WorksheetExists(sheetName As String, wb As Workbook) As Boolean
    On Error Resume Next
    WorksheetExists = Not wb.Sheets(sheetName) Is Nothing
    On Error GoTo 0
End Function
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try moving this line:
VBA Code:
Application.DisplayAlerts = True
Below this part:
VBA Code:
    With newWB
        .SaveAs Filename:=sFile, FileFormat:=51
        .Close savechanges:=False
    End With
 
Upvote 0
Thanks GeorgiBoy. This works perfectly regarding supressing the message

Where H2 onwards is blank, I need the code amended to Exit Sub. However if H2 onwards is blank, it creates a workbook with sheet1, which I don't want if there is nothing in this column on sheet Macro
 
Upvote 0
Try moving this part:
VBA Code:
    Dim newWB As Workbook ' Create a new workbook to copy the sheets
    Set newWB = Workbooks.Add
After this part:
VBA Code:
    ' Check if column H is empty or not visible
    Dim lastRowH As Long
    lastRowH = wsMacro.Cells(wsMacro.Rows.Count, "H").End(xlUp).Row
   
    If lastRowH < 2 Or wsMacro.Columns("H").Hidden Then
        MsgBox "No sheets to attach.", vbInformation
        Exit Sub
    End If

That way it will Exit Sub before it creates the new workbook
 
Upvote 0
Solution
Many Thanks Georgiboy

Your suggestion solved the issue
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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