Email Excel Tabs Macro

mnentertain

New Member
Joined
Nov 1, 2014
Messages
2
I need a macro that sends each tab in the worksheet to an email address listed in the cell of that worksheet. If it doesn't then I need the tab to be skipped. I also need the worksheets that have been sent to be saved in a specific folder in different files and the tabs that haven't been emailed to also be saved in separate files in another folder. Really appreciate any help.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
.
.

Try something like this:

Code:
Sub Call_Sub()

    Call SaveAndEmailWorksheets(ActiveWorkbook)

End Sub

'----------------------------------------------------------------------

Function bIsEmailAddress(sEmailAddress As String) As Boolean

    'returns TRUE if the argument is a
    'valid email address and FALSE if not

    Dim vComponents As Variant
    vComponents = Split(sEmailAddress, "@")
    
    If UBound(vComponents) <> 1 Then
        bIsEmailAddress = False
    ElseIf InStr(1, vComponents(1), ".") = 0 Then
        bIsEmailAddress = False
    Else
        bIsEmailAddress = True
    End If

End Function

'----------------------------------------------------------------------

Sub SaveAndEmailWorksheets(wkbParent As Workbook)

    'Copies a worksheet into one folder if the specified cell
    'contains an email address and another folder if not.
    
    'Also emails a worksheet to its recipient if there is
    'indeed an email address in the specified cell.

    Const sEMAIL_CELL As String = "A1"
    Const sSAVE_PATH_1 As String = "C:\Users\Greg\Desktop\Folder1\"
    Const sSAVE_PATH_2 As String = "C:\Users\Greg\Desktop\Folder2\"
    
    Dim wksToSave As Worksheet
    Dim sSavePath As String
    Dim bSendMail As Boolean
    
    For Each wksToSave In wkbParent.Worksheets
        
        bSendMail = bIsEmailAddress( _
            wksToSave.Range(sEMAIL_CELL).Value)
        
        If bSendMail Then
            sSavePath = sSAVE_PATH_1
        Else
            sSavePath = sSAVE_PATH_2
        End If
        
        wksToSave.Copy
        
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs _
            Filename:=sSavePath & wksToSave.Name & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook
        Application.DisplayAlerts = True
        
        If bSendMail Then
            ActiveWorkbook.SendMail _
                Recipients:=Range(sEMAIL_CELL).Value, _
                Subject:=ActiveWorkbook.Name
        End If
        
        ActiveWorkbook.Close
        
    Next wksToSave

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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