Select cells in a worksheet, import to another worksheet, sort on criteria, and create and send a spreadsheet as an email attachment

juscuz419

Board Regular
Joined
Apr 18, 2023
Messages
59
Office Version
  1. 2019
Platform
  1. Windows
I have a workbook that I need to select rows that have cells numbered in one worksheet, import them into another worksheet, sort them based on a value in one column,

The first worksheet can be 150 rows long and would look like the table below (with data in columns C through O too)


ARoleCDEFGHIJKLMNO
1AM
A
3TL1
A
5A
4D
D
2D
D
A
7TL2
A
8D
A
6D


I need it to import into a second worksheet with the results to look like this (with the C through O data too)


ARoleCDEFGHIJKLMNO
1AM
2D
3TL1
4D
5A
6D
7TL2
8D


And then create a spreadsheet of the result, open outlook, attach the new sheet, and allow the input of the addressees.

I've tried for a while but this has me befuddled.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this:

VBA Code:
Sub Create_And_Send_Sheet()
  Dim sh2 As Worksheet
  Dim pathFile As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  pathFile = ThisWorkbook.Path & "\" & "File.xlsx"  'fit to the name of new workbook
  Sheets("Sheet1").Copy                             'fit to the name of sheet to send
  Set sh2 = ActiveSheet
  
  sh2.Range("A:O").Sort sh2.Range("A1"), xlAscending, Header:=xlYes
  sh2.Range("A" & sh2.Range("A" & Rows.Count).End(3).Row + 1 & ":O" & Rows.Count).Clear
  sh2.Parent.SaveAs pathFile, xlOpenXMLWorkbook
  sh2.Parent.Close False
   
  With CreateObject("Outlook.Application").CreateItem(0)
    .To = "example@gmail.com"
    .Subject = "This is the Subject line"
    .Body = "This is the BODY line"
    .Attachments.Add pathFile
    .Display
  End With
  
  Application.ScreenUpdating = False
End Sub

🤗
 
Upvote 0
Solution
Got called away. Sorry for the delay. Worked like a charm. You have helped me with issues in the past. You are great. Thanks
 
Upvote 0

Forum statistics

Threads
1,225,619
Messages
6,186,049
Members
453,335
Latest member
sfd039

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