SOLVED
Home

Macro to send Tab of Excel As a CSV via email

%3CLINGO-SUB%20id%3D%22lingo-sub-1191884%22%20slang%3D%22en-US%22%3EMacro%20to%20send%20Tab%20of%20Excel%20As%20a%20CSV%20via%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1191884%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20daily%20task%20which%20requires%20me%20to%20get%20data%20from%20a%20pdf%20each%20day%20and%20then%20send%20as%20a%20CSV%20file%20to%20another%20email.%20I%20am%20looking%20to%20have%20a%20macro%20to%20be%20able%20to%20send%20one%20tab%20of%20an%20excel%20as%20a%20CSV%20file%20to%20the%20email%20address%20listed%20on%20the%20tab.%20From%20my%20research%20i%20have%20found%20the%20below%20macro%20to%20send%20a%20tab%20as%20CSV.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPublic%20Sub%20Email_Sheets_As_CSV()%0A%0A%20%20%20%20Dim%20csvFiles(1%20To%203)%20As%20String%2C%20i%20As%20Integer%0A%20%20%20%20Dim%20wsName%20As%20Variant%0A%20%20%20%20Dim%20OutApp%20As%20Object%2C%20OutMail%20As%20Object%0A%20%20%20%20%0A%20%20%20%20i%20%3D%200%0A%20%20%20%20For%20Each%20wsName%20In%20Array(%22Sheet1%22%2C%20%22Sheet2%22%2C%20%22Sheet3%22)%20%20%20%20%20'sheet%20names%20to%20be%20emailed%20-%20CHANGE%20THE%20NAMES%0A%20%20%20%20%20%20%20%20i%20%3D%20i%20%2B%201%0A%20%20%20%20%20%20%20%20csvFiles(i)%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20wsName%20%26amp%3B%20%22.csv%22%0A%20%20%20%20%20%20%20%20ThisWorkbook.Worksheets(wsName).Copy%0A%20%20%20%20%20%20%20%20ActiveWorkbook.SaveAs%20csvFiles(i)%2C%20FileFormat%3A%3DxlCSV%0A%20%20%20%20%20%20%20%20ActiveWorkbook.Close%20False%0A%20%20%20%20Next%0A%0A%20%20%20%20'Email%20the%20.csv%20files%0A%20%20%20%20%0A%20%20%20%20Set%20OutApp%20%3D%20CreateObject(%22Outlook.Application%22)%0A%20%20%20%20Set%20OutMail%20%3D%20OutApp.CreateItem(0)%0A%0A%20%20%20%20With%20OutMail%0A%20%20%20%20%20%20%20%20.To%20%3D%20ThisWorkbook.Worksheets(%22Settings%22).Range(%22A1%22).Value%20%20%20%20%20'cell%20containing%20email%20address%20-%20CHANGE%20THE%20SHEET%20%26amp%3B%20CELL%0A%20%20%20%20%20%20%20%20.CC%20%3D%20%22%22%0A%20%20%20%20%20%20%20%20.BCC%20%3D%20%22%22%0A%20%20%20%20%20%20%20%20.Subject%20%3D%20%22Email%20subject%20here%22%0A%20%20%20%20%20%20%20%20.Body%20%3D%20%22This%20email%20contains%203%20.csv%20file%20attachments.%22%0A%20%20%20%20%20%20%20%20.Attachments.Add%20csvFiles(1)%0A%20%20%20%20%20%20%20%20.Attachments.Add%20csvFiles(2)%0A%20%20%20%20%20%20%20%20.Attachments.Add%20csvFiles(3)%0A%20%20%20%20%20%20%20%20.Send%0A%20%20%20%20End%20With%0A%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20%0A%20%20%20%20Set%20OutMail%20%3D%20Nothing%0A%20%20%20%20Set%20OutApp%20%3D%20Nothing%0A%20%20%20%20%0A%20%20%20%20'Delete%20the%20.csv%20files%0A%20%20%20%20%0A%20%20%20%20Kill%20csvFiles(1)%0A%20%20%20%20Kill%20csvFiles(2)%0A%20%20%20%20Kill%20csvFiles(3)%0A%20%20%20%20%0AEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3BCan%20someone%20please%20assist%20in%20updating%20this%20to%20so%20it%20reads%20the%20tab%20%22Upload%20CSV%22%20to%20send%20an%20email%20address%20listed%20in%20cell%20B5%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20kindly%20for%20any%20assistance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1191884%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECharting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20for%20web%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20Mac%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%20on%20mobile%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPower%20BI%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1206682%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20send%20Tab%20of%20Excel%20As%20a%20CSV%20via%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1206682%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F364226%22%20target%3D%22_blank%22%3E%40calof1%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETry%20below%20code.%20This%20module%20is%20programmed%20to%20use%20MS%20Outlook%20for%20the%20email%20and%20is%20set%20to%20activate%20and%20run%20only%20for%20sheet%20named%20Upload%20CSV.%20I%20don't%20have%20Outlook%20so%20was%20not%20able%20to%20fully%20test%20the%20code%2C%20however%2C%20the%20.csv%20file%20is%20created%20%2F%20saved%20successfully.%20I%20also%20changed%20the%20send%20function%20to%20'display'%20for%20testing%20purposes%20but%20you%20can%20switch%20this%20back%20once%20you're%20happy%20it%20works%20as%20anticipated.%20Hope%20this%20is%20works%20for%20you%3A%3CBR%20%2F%3E%3CBR%20%2F%3EPublic%20Sub%20Email_Sheets_As_CSV()%3CBR%20%2F%3E%3CBR%20%2F%3EDim%20csvFile%20As%20String%3CBR%20%2F%3EDim%20wsName%20As%20Variant%3CBR%20%2F%3EDim%20OutApp%20As%20Object%2C%20OutMail%20As%20Object%3CBR%20%2F%3E%3CBR%20%2F%3ESheets(%22Upload%20CSV%22).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EwsName%20%3D%20ActiveSheet.Name%3CBR%20%2F%3EcsvFile%20%3D%20ThisWorkbook.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20wsName%20%26amp%3B%20%22.csv%22%3CBR%20%2F%3EThisWorkbook.ActiveSheet.Copy%3CBR%20%2F%3EActiveWorkbook.SaveAs%20csvFile%2C%20FileFormat%3A%3DxlCSV%3CBR%20%2F%3EActiveWorkbook.Close%20False%3CBR%20%2F%3E%3CBR%20%2F%3E'Email%20the%20.csv%20files%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20OutApp%20%3D%20CreateObject(%22Outlook.Application%22)%3CBR%20%2F%3ESet%20OutMail%20%3D%20OutApp.CreateItem(0)%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20OutMail%3CBR%20%2F%3E.To%20%3D%20ThisWorkbook.Worksheets(%22Upload%20CSV%22).Range(%22B5%22).Value%20'cell%20containing%20email%20address%20-%20CHANGE%20THE%20SHEET%20%26amp%3B%20CELL%3CBR%20%2F%3E.CC%20%3D%20%22%22%3CBR%20%2F%3E.BCC%20%3D%20%22%22%3CBR%20%2F%3E.Subject%20%3D%20%22Email%20subject%20here%22%3CBR%20%2F%3E.Body%20%3D%20%22This%20email%20contains%201%20.csv%20file%20attachments.%22%3CBR%20%2F%3E.Attachments.Add%20csvFile%3CBR%20%2F%3E.Display%20'change%20to%20.Send%20to%20send%20email%20without%20displaying%20first%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EOn%20Error%20GoTo%200%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20OutMail%20%3D%20Nothing%3CBR%20%2F%3ESet%20OutApp%20%3D%20Nothing%3CBR%20%2F%3E%3CBR%20%2F%3E'Delete%20the%20.csv%20files%3CBR%20%2F%3E%3CBR%20%2F%3EKill%20csvFile%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1208090%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20send%20Tab%20of%20Excel%20As%20a%20CSV%20via%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1208090%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531239%22%20target%3D%22_blank%22%3E%40Charla74%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20greatly%20for%20your%20help%20with%20this.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20update%20and%20complete%20some%20testing%20shortly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again%20for%20your%20help%2C%20much%20appreciated.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

Hi,

 

I have a daily task which requires me to get data from a pdf each day and then send as a CSV file to another email. I am looking to have a macro to be able to send one tab of an excel as a CSV file to the email address listed on the tab. From my research i have found the below macro to send a tab as CSV.

 

 

Public Sub Email_Sheets_As_CSV()

    Dim csvFiles(1 To 3) As String, i As Integer
    Dim wsName As Variant
    Dim OutApp As Object, OutMail As Object
    
    i = 0
    For Each wsName In Array("Sheet1", "Sheet2", "Sheet3")     'sheet names to be emailed - CHANGE THE NAMES
        i = i + 1
        csvFiles(i) = ThisWorkbook.Path & "\" & wsName & ".csv"
        ThisWorkbook.Worksheets(wsName).Copy
        ActiveWorkbook.SaveAs csvFiles(i), FileFormat:=xlCSV
        ActiveWorkbook.Close False
    Next

    'Email the .csv files
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = ThisWorkbook.Worksheets("Settings").Range("A1").Value     'cell containing email address - CHANGE THE SHEET & CELL
        .CC = ""
        .BCC = ""
        .Subject = "Email subject here"
        .Body = "This email contains 3 .csv file attachments."
        .Attachments.Add csvFiles(1)
        .Attachments.Add csvFiles(2)
        .Attachments.Add csvFiles(3)
        .Send
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    'Delete the .csv files
    
    Kill csvFiles(1)
    Kill csvFiles(2)
    Kill csvFiles(3)
    
End Sub

 Can someone please assist in updating this to so it reads the tab "Upload CSV" to send an email address listed in cell B5?

 

Thank you kindly for any assistance.

2 Replies
Highlighted
Solution
@calof1

Try below code. This module is programmed to use MS Outlook for the email and is set to activate and run only for sheet named Upload CSV. I don't have Outlook so was not able to fully test the code, however, the .csv file is created / saved successfully. I also changed the send function to 'display' for testing purposes but you can switch this back once you're happy it works as anticipated. Hope this is works for you:

Public Sub Email_Sheets_As_CSV()

Dim csvFile As String
Dim wsName As Variant
Dim OutApp As Object, OutMail As Object

Sheets("Upload CSV").Activate

wsName = ActiveSheet.Name
csvFile = ThisWorkbook.Path & "\" & wsName & ".csv"
ThisWorkbook.ActiveSheet.Copy
ActiveWorkbook.SaveAs csvFile, FileFormat:=xlCSV
ActiveWorkbook.Close False

'Email the .csv files

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

With OutMail
.To = ThisWorkbook.Worksheets("Upload CSV").Range("B5").Value 'cell containing email address - CHANGE THE SHEET & CELL
.CC = ""
.BCC = ""
.Subject = "Email subject here"
.Body = "This email contains 1 .csv file attachments."
.Attachments.Add csvFile
.Display 'change to .Send to send email without displaying first
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

'Delete the .csv files

Kill csvFile

End Sub
Highlighted

Hi@Charla74 

 

Thank you greatly for your help with this. 

 

I will update and complete some testing shortly.

 

Thanks again for your help, much appreciated.

Related Conversations