+ Reply to Thread
Results 1 to 4 of 4

Email sheets macro errors if no sheets match

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Email sheets macro errors if no sheets match

    Hi,

    I'm using a macro which looks for sheets in a workbook with a specific name and emails the sheets to a recipient.

    However, if the macro fails to find a matching sheet - the code will error.
    Does anyone know how it might be modified to skip running the macro if none are found

    Here's the code:

    Sub EmailHearnNC()
    ContinueEmail:
    Application.DisplayAlerts = False
      '1 All
    Dim sh, arr() As Variant
    Dim bDim As Boolean: bDim = False
    
    For Each sh In Array("61")
        If Evaluate("=ISREF('" & sh & "'!A1)") Then
            If bDim = False Then
                ReDim arr(0 To 0) As Variant
                arr(0) = sh
                bDim = True
            Else
                ReDim Preserve arr(0 To UBound(arr) + 1) As Variant
                arr(UBound(arr)) = sh
            End If
        End If
    Next sh
    
    Sheets(arr).Select
    Sheets(arr).Copy
    
    ChDir "U:\"
    ActiveWorkbook.SaveAs Filename:="U:\testfile.xlsx", FileFormat:=51
    ActiveWorkbook.SendMail Recipients:= _
    Array("test@test.com"), _
    Subject:="test" & Eom
    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close
    
    Dim myVal
    On Error Resume Next
    Set QueryOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    myVal = Shell("C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\OUTLOOK.EXE", 1)
    Exit Sub
    Else
    End If
    
    End Sub
    In this example the code will error at "Sheets(arr).Select".

    If anyone can offer any advice it would be much appreciated!
    Thanks.
    Last edited by BPSJACK; 01-18-2016 at 01:21 PM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Email sheets macro errors if no sheets match

    May be just add checking if anything found?


    Dim there_were_sheets as boolean
    
    '...
    
    there_were_sheets = False
    For Each sh In Array("61")
        If Evaluate("=ISREF('" & sh & "'!A1)") Then
            there_were_sheets = True
            If bDim = False Then
    '...
            End If
       End If
    Next sh
    
    If there_were_sheets Then
      Sheets(arr).Select
      Sheets(arr).Copy
    
    '...
    
    Else
      MsgBox "Nothing to send!", vbCritical, "Sorry"
    End If
    Best Regards,

    Kaper

  3. #3
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Email sheets macro errors if no sheets match

    Sub EmailHearnNC()
    ContinueEmail:
    Application.DisplayAlerts = False
      '1 All
    Dim sh, arr() As Variant
    Dim bDim As Boolean: bDim = False
    
    x=0
    For Each sh In Array("61")
        If Evaluate("=ISREF('" & sh & "'!A1)") Then
            x=1
            If bDim = False Then
                ReDim arr(0 To 0) As Variant
                arr(0) = sh
                bDim = True
            Else
                ReDim Preserve arr(0 To UBound(arr) + 1) As Variant
                arr(UBound(arr)) = sh
            End If
        End If
    Next sh
    
    if x=0 then
         msgbox "Sheet not found"
         Exit sub
    endif
    Sheets(arr).Select
    Sheets(arr).Copy
    
    ChDir "U:\"
    ActiveWorkbook.SaveAs Filename:="U:\testfile.xlsx", FileFormat:=51
    ActiveWorkbook.SendMail Recipients:= _
    Array("test@test.com"), _
    Subject:="test" & Eom
    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close
    
    Dim myVal
    On Error Resume Next
    Set QueryOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    myVal = Shell("C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE\OUTLOOK.EXE", 1)
    Exit Sub
    Else
    End If
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Re: Email sheets macro errors if no sheets match

    Thanks guys!

    brokenbiscuits I copied and pasted that sub directly and it worked perfectly

    Thanks again.

+ 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. Macro to email individual sheets to employees
    By NeoDasari in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-23-2015, 09:24 AM
  2. [SOLVED] Search Columns from Multiple Sheets If Match Populate Value from 1 of the sheets
    By kellyjo7 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-08-2014, 05:52 PM
  3. Macro to search email address in all sheets
    By kishoremcp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-22-2014, 04:57 AM
  4. macro email two sheets in one file to user
    By matrix_xrs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 01:45 PM
  5. Application.Match with two sheets help. Getting different errors with every change.
    By djrudman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-16-2013, 11:47 AM
  6. Macro to Email Multiple Sheets
    By regularmark in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-06-2011, 04:20 PM
  7. Macro to email individual spread sheets
    By lissa843 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2010, 10:38 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