Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Using Macro to Cells in Excel

Nov 11, 2004 7:48AM PST

I have a macro set up which copies cells from Sheet1 to Sheet2. I would like to add to the macro and have those cells copied (A2:C15) and then paste them into an email to send. I have tried to use the macro recorder to send the sheet (Not really what I want to do though) and it doesn't show anything in the recording. Can you help?

Discussion is locked

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 4:53PM PST

Add this code to your macro:

Sub DefinedAreaToEmail()

Dim ToYou As String 'The receiver of the email

Application.SheetsInNewWorkBook = 1
ToYou = InputBox("Please type the name of the receiver of this email.")
If ToYou = "" Then Exit Sub
Range("A2:C15").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs "Attachment.xls"
Application.Dialogs(xlDialogSendMail).Show ToYou, "Updated Attachment"

End Sub

Hope this works.

Swisse

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 5:14PM PST

JSteinmetz,

Let me suggest a simple workaround. You add a 'copy to clipboard' line in the macro (can be done simply with the macro recorder). Then paste it into your email yourself.

The Excel macro is limited to Excel-commands, and won't work after you choose File>Send to...
You can automate the whole process of sending a mail, but that requires a lot of programming and moreover you explicitly state you don't want it.

Hope this helps.


Kees

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 5:55PM PST

If I could get the macro to copy the specified cells and paste it into an email, that is exactly what I would want. I wouldn't want to make it an attachment but just a simple copy/paste into an email would be all that I need.

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 6:13PM PST

You haven't tried out yet the macro that i wrote.

Swisse

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 7:57PM PST

Jsteinmetz,

Tell more. You would expect the macro to 'know' what email to pick (you could have different emails open, of course, or possibly none at all, in Outlook or Outlook Express or Incredimail or Eudora or Netscape or Thunderbird or whatever email-program you use) and then 'know' at what exact location on your mail to paste it.
That isn't a very reasonable expectation, I would say. So maybe you can make your wishes more clear?

In the meantime you might like to read http://support.microsoft.com/default.aspx?scid=kb;en-us;161833
It's about sending mails from Visual Basic, and parts of it might be transferable to your macro. As I said: you'll need to program.

Kees

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 8:35PM PST

I tried the code Swisse supplied and it worked but it did make an attachmentSad Now that I am thinking about it, If I was able to just make it hit File, Send to Mail Recipient that would be sufficient. I will have other people using the sheet and macro's I made so I would like to make it as simple as possible. I'm not sure if that is possible.

- Collapse -
Re: Using Macro to Cells in Excel
Nov 11, 2004 9:48PM PST

Sending a Selection of Cells in Excel without sending it as a sheet is impossible if your using VBA. Maybe it's possible if you cut the selection and paste it in the clipboard and send it manually. The following code that I'll post sends the selected cells as a sheet, but strips it of the macro it contains. So you don't have to worry about somebody else reading your code. Again here is the code:

Sub DefinedAreaToEmail()
Dim ToYou As String 'The receiver of the email
On Error GoTo theend
Application.SheetsInNewWorkbook = 1
ToYou = InputBox("Please type the name of the receiver of this email.")
If ToYou = "" Then Exit Sub
Original = ActiveWorkbook.Name
JustCopy = "Attachment.xls"
Range("A2:C15").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs JustCopy
Workbooks(Original).Activate
For i = 1 To Sheets.Count
Sheets(i).Copy After:=Workbooks(JustCopy).Sheets(i)
Next
Application.Dialogs(xlDialogSendMail).Show ToYou, "Updated Attachment"
theend:
End Sub

Swisse

- Collapse -
Re: Using Macro to Cells in Excel
Nov 12, 2004 12:34AM PST

Ok, thank you for the help. I will give this a try.

- Collapse -
Re: Using Macro to Cells in Excel
Nov 12, 2004 6:38PM PST

Sorry for some bugs in the code. Please correct the following:

1. Replace On Error Go To theend with On Error Resume Next.
2. Erase theend:.

Thanks,
Swisse

- Collapse -
Re: Using Macro to Cells in Excel
Nov 12, 2004 10:24PM PST

Well, what I ended up doing is just run my macro that copies the needed information to sheet2 and let it stop there. I have removed the grid lines from that page. I'm going to just use the send to mail recipient which will end up giving me the thing that I wanted. It's an extra step but it's like only 3 seconds. Thank you both for your help.