Macro to extract data from sheet and email this

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,570
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to extract data from sheet "Man accounts bank" to a blank workbook and then to save this worksheet containing this sheet only to C:/my Documents and then to attach the file to be emailed

Code:
 Sub SendFiles()
    Dim lCount As Long
    Dim vFilenames As Variant
    Dim sPath As String
    Dim lFilecount As Long
    Dim sFullName As String
    sPath = "C:\My Documents\"
    ChDrive sPath
    ChDir sPath
   
        Application.DisplayAlerts = False
        Application.CutCopyMode = False
       Sheets(Array("Man Accounts Bank")).Copy
        ActiveWorkbook.SaveAs Replace(vFilenames(lCount), ".xls", "") & ".Man Accounts bank.xls", FileFormat:=xlNormal
        vFilenames(lCount) = ActiveWorkbook.FullName
        Application.ScreenUpdating = False
        For Each sht In Sheets(Array("Man Accounts bank"))
        Sheets(sht.Name).UsedRange.Copy
        Sheets(sht.Name).Range("a1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Next
        Application.ScreenUpdating = True
        ActiveWorkbook.Close True
        Application.DisplayAlerts = True
        Application.CutCopyMode = True
   
      'Mailfiles "", vFilenames
    For lCount = LBound(vFilenames) To UBound(vFilenames)
Kill vFilenames(lCount)
Next
   ActiveWorkbook.Close False
End Sub

When running the macro, type mismatch 13 appears and the following code is highlighted

Code:
 ActiveWorkbook.SaveAs Replace(vFilenames(lCount), ".xls", "") & ".Man Accounts bank.xls", FileFormat:=xlNormal

Your assistance in resolving this is most appreciated
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I have also posted this on

Macro to extract sheet and email | Windows Secrets Lounge

See additional code for emailing which I omitted from my earlier post


Code:
 Sub Mailfiles(sMailAddress As String, vFiles As Variant)
    Dim oMailItem As Object
    Dim oOLapp As Object
    Dim lCt As Long

    Set oOLapp = CreateObject("Outlook.application")
    Set oMailItem = oOLapp.CreateItem(0)
    With oMailItem
        .To = sMailAddress
        .Subject = "Group Accounts"
       .body = "Attached please find Group accounts as at " & Format(Month(Date) - 1 & " " & Year(Date), "mmm yyyy") & " & vbNewLine & vbNewLine"
       
                  
              .body = .body & "Regards" & vbNewLine & vbNewLine
.body = .body & "Howard"

 For lCt = LBound(vFiles) To UBound(vFiles)
            .attachments.Add CStr(vFiles(lCt))
        Next
        .Display
        Set oOLapp = Nothing
        Set oMailItem = Nothing
    End With
   
      
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,842
Messages
6,127,235
Members
449,372
Latest member
charlottedv

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