need Outlook Macro to copy range from cell A2 of an email attachment to last row used and paste it to a specific excel file in C drive

Latha

Board Regular
Joined
Feb 24, 2011
Messages
146
Team,

I have the below codes which I copied from google and edited a bit. but not working as per my requirement.

the objective is to :

identify an email in inbox with a specific subject line "Ageing Report" --> copy the ranges in that report from cell A2 to till end of the row which contains data --> paste it in the cell "A6" onwards in the specific local file "C:\Users\dlatha\Desktop\Collate.xlsx" --> and delete all the cells which are not relevant (for example : after copying data copied till row 565 the macro should delete/clear contents from row 566 onwards)

As said I copied the below codes from google and bit confused where to edit and what to edit to suit my requirement.

Kindly help.

Sub GetAttachmentdata()
Dim olitem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlTempWB As Object
Dim xlSheet As Object
Dim xlTempSheet As Object
Dim lngTempLast As Integer
Dim lngLast As Integer
Dim strFname As String
Dim strTempPath As String
Dim bXLStarted As Boolean


Const strPath As String = "C:\Users\dlatha\Desktop\NewExcel.xlsx" 'the path and name of the local workbook
strTempPath = Left(strPath, InStrRev(strPath, "\")) 'The path of the temporary file

On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Set xlApp = CreateObject("Excel.Application")
bXLStarted = True
End If
xlApp.Visible = True

On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1") 'The sheet in the local workbook

'Process the message attachment
With olitem.Attachments.Item(1)
If Right(.DisplayName, 4) = "xlsx" Then
lngLast = xlSheet.Range("A" & xlSheet.Rows.Count).End(-4162).Row
strFname = strTempPath & .DisplayName
.SaveAsFile strFname
Set xlTempWB = xlApp.Workbooks.Open(strFname, editable:=True)
Set xlTempSheet = xlTempWB.Sheets("Report 1")
lngTempLast = xlTempSheet.Range("B" & xlTempSheet.Rows.Count).End(-4162).Row
xlSheet.Range("A" & lngLast + 1, "S" & lngLast + lngTempLast - 1).Value = xlTempSheet.Range("A2", "S" & lngTempLast).Value
xlWB.Save
End If

End With
xlWB.Close SaveChanges:=True
xlTempWB.Close SaveChanges:=False
If bXLStarted Then
xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set xlTempWB = Nothing
Set xlTempSheet = Nothing
Set olitem = Nothing
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

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