Consulting

Results 1 to 7 of 7

Thread: How do I select more than one sheet and send as workbook

  1. #1
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location

    Smile How do I select more than one sheet and send as workbook

    Option Explicit 
     
    Sub EmailandSaveCellValue() 
    'Variable declaration
    Dim oApp As Object, _ 
    oMail As Object, _ 
    WB As Workbook, _ 
    FileName As String, MailSub As String, MailTxt As String 
    'Set email details; Comment out if not required
    Const MailTo = "some1@someone.com" 
    Const MailCC = "some2@someone.com" 
    Const MailBCC = "some3@someone.com" 
    MailSub = "Please review " & Range("Subject") 
    MailTxt = "I have attached " & Range("Subject") 
    'Turns off screen updating
    Application.ScreenUpdating = False 
    'Makes a copy of the active sheet and save it to
    'a temporary file
    ActiveSheet.Copy 
    Set WB = ActiveWorkbook 
    FileName = Range("Subject") & " Text.xls" 
    On Error Resume Next 
    Kill "C:\" & FileName 
    On Error Goto 0 
    WB.SaveAs FileName:="C:\" & FileName 
    'Creates and shows the outlook mail item
    Set oApp = CreateObject("Outlook.Application") 
    Set oMail = oApp.CreateItem(0) 
    With oMail 
    .To = MailTo 
    .Cc = MailCC 
    .Bcc = MailBCC 
    .Subject = MailSub 
    .Body = MailTxt 
    .Attachments.Add WB.FullName 
    .Display 
    End With 
    'Deletes the temporary file
    WB.ChangeFileAccess Mode:=xlReadOnly 
    Kill WB.FullName 
    WB.Close SaveChanges:=False 
    'Restores screen updating and release Outlook
    Application.ScreenUpdating = True 
    Set oMail = Nothing 
    Set oApp = Nothing 
    End Sub
    Last edited by Erays; 03-18-2005 at 05:41 PM. Reason: Need help

  2. #2
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    What is the criteria for the Sheets you want to select, eg 1st 3 sheets, sheets that start with Send ?? etc.
    Kind Regards,
    Ivan F Moala From the City of Sails

  3. #3
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location
    I just want to be able to select the sheets and run the macro

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Erays
    I just want to be able to select the sheets and run the macro
    How do you want to select the sheets?

    1. by holding down the Cntl Key and clicking on the tab names?
    2. by selecting from a form?
    3. some other method?

    Assuming you wish to use #1, you need to insert a loop that will copy all selected sheets to the target workbook. As an example of processing a selection of multiple worksheets, the following code will display the names for all selected sheets:


    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Windows(1).SelectedSheets
            MsgBox ws.Name
        Next ws

  5. #5
    MS Excel MVP VBAX Tutor
    Joined
    Mar 2005
    Posts
    246
    Location
    Who needs a loop?

    ActiveWorkbook.Windows(1).SelectedSheets.Copy
    This copies the selected sheets into a new workbook.

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______

  6. #6
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    This method may not be what you want, it uses SendMail, but it works for me. This won't work for you if you really need to put text in the message body, etc..
    but you specify the sheets in the Array, specify the email address', then run it. Obviously this assumes sheet names and address' are static, if it's not what your looking for, disregard.

    Option Explicit 
    
    Sub TwoSheetsAndYourOut()
        Dim NewName As String
        Dim nm As Name
        Dim ws As Worksheet
    If MsgBox("Copy specific sheets to a new workbook" & vbCr & _
        "New sheets will be pasted as values, named ranges removed" _
        , vbYesNo, "NewCopy") = vbNo Then Exit Sub
        With Application
            .ScreenUpdating = False
    '       Copy specific sheets
    '       *SET THE SHEET NAMES TO COPY BELOW*
    '       Array("Sheet Name", "Another sheet name", "And Another"))
    '       Sheet names go inside quotes, seperated by commas
            On Error GoTo ErrCatcher
            Sheets(Array("Copy Me", "Copy Me2")).Copy
    '       Paste sheets as values
            For Each ws In ActiveWorkbook.Worksheets
                ws.[A1:IV65536].Copy
                ws.[A1].PasteSpecial Paste:=xlValues
            Next ws
            Application.CutCopyMode = False
    '       Remove named ranges
            For Each nm In ActiveWorkbook.Names
                nm.Delete
            Next nm
    '       Save it with the name "Test" and in the same directory as original
            With ActiveWorkbook
                .SaveAs ThisWorkbook.Path & "\test.xls"
                .SendMail "MailAddy@you.com", "Subject Here"
                .Close False
            End With
            Kill ThisWorkbook.Path & "\test.xls"
    .ScreenUpdating = True
        End With
        Exit Sub
    ErrCatcher:
        MsgBox "Specified sheets do not exist within this workbook"
    End Sub

  7. #7
    VBAX Regular Erays's Avatar
    Joined
    Mar 2005
    Posts
    73
    Location

    Thumbs up Highlight pages and send as workbook

    This is the one that works for me. It emails from a value in a cell and names the book from a value in a cell.

    Sub EmailWithOutlookBook()
    'Variable declaration
    Dim oApp As Object, _
    oMail As Object, _
    WB As Workbook, _
    FileName As String
    'Turns off screen updating
    Application.ScreenUpdating = False
    'Makes a copy of the active sheet and save it to
    'a temporary file
    ActiveWorkbook.Windows(1).SelectedSheets.Copy
    Set WB = ActiveWorkbook
    FileName = Cells(8, 3).Value & " Reis.xls"
    On Error Resume Next
    Kill "C:\" & FileName
    On Error GoTo 0
    WB.SaveAs FileName:="C:\" & FileName
    'Creates and shows the outlook mail item
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
    With oMail
    'Uncomment the line below to hard code a recipient
    .To = Cells(7, 11).Value
    .Cc = Cells(8, 11).Value
    .Bcc = Cells(9, 11).Value
    'Uncomment the line below to hard code a subject
    .Subject = "Here are the reis In workbook form, if you have questions please call "
    '.Body = "I Have attached to this email the totol loss template for claim Number " & Cells(6, 3).Value
    .Attachments.Add WB.FullName
    .Display
    End With
    'Deletes the temporary file
    WB.ChangeFileAccess Mode:=xlReadOnly
    Kill WB.FullName
    WB.Close SaveChanges:=False
    'Restores screen updating and release Outlook
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
    End Sub
    Last edited by Erays; 03-19-2005 at 06:58 AM. Reason: Took out personal information

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •