Share via

Excel Macro To Send Email & Delete Data

Anonymous
2018-06-12T13:57:12+00:00

I am testing using macros to accomplish having a single Excel button send the file (as attachment or raw data) to a recipient and then delete all the data entered. The Excel worksheet in question is a template and has all cells locked except for editable cells.

So far I have added 2 buttons - one to email and one to clear the user's inputted data.

Can anyone recommend a simpler, more streamlined solution? Like a single button to email the data then clear the data?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Vijay A. Verma 104.9K Reputation points Volunteer Moderator
    2018-06-12T15:22:18+00:00

    Use Cells.Clear

    It will clear the entire worksheet. Do you want to clear only limited cells, then you need to define that range and then clear?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-06-12T15:14:31+00:00

    I've tried many variations of what you see below, none have worked.

    Was this answer helpful?

    0 comments No comments
  3. Vijay A. Verma 104.9K Reputation points Volunteer Moderator
    2018-06-12T15:05:28+00:00

    Just put Clearcells before Endsub of first routine.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2018-06-12T14:20:53+00:00

    I have this so far:

    Private Sub CommandButton2_Click()

        Dim xOutApp As Object

        Dim xOutMail As Object

        Dim xMailBody As String

        On Error Resume Next

        Set xOutApp = CreateObject("Outlook.Application")

        Set xOutMail = xOutApp.CreateItem(0)

        xMailBody = "Body content" & vbNewLine & vbNewLine & _

                  "This is line 1" & vbNewLine & _

                  "This is line 2"

                      On Error Resume Next

        With xOutMail

            .To = "******@domain.com"

            .CC = ""

            .BCC = ""

            .Subject = "Test email send by button clicking"

            .Body = xMailBody

            .Attachments.Add ActiveWorkbook.FullName

            .Display   'or use .Send

        End With

        On Error GoTo 0

        Set xOutMail = Nothing

        Set xOutApp = Nothing

    End Sub

    Private Sub CommandButton3_Click()

    Clearcells

    End Sub

    How would I combine the scripts into 1 single button?

    Was this answer helpful?

    0 comments No comments
  5. Vijay A. Verma 104.9K Reputation points Volunteer Moderator
    2018-06-12T14:01:11+00:00

    The code which you have generated for deletion, you should be able to copy and paste this code into Send Mail code at the end.

    Was this answer helpful?

    0 comments No comments