Outlook Email Content to Excel (VBA)

SerenityNetworks

Board Regular
Joined
Aug 13, 2009
Messages
131
Office Version
  1. 365
Platform
  1. Windows
I'm not a coder, but I'm trying to merge a couple bits of VBA I've found in order to copy the body of emails in a directory into Excel. The code is below.

Really, all I need is:

  • The date/time stamp of the email.
  • The body of the email.
  • From a specified Outlook folder.
All else is a nice-to-have.

To be more specific, the body of the email will always contain a single line of text followed by a seven column table that is n rows (where n is at least 2 and is never greater than 50). The table has a header row and then n data rows. The data rows in the table may have line breaks within a given cell. (Note: If I 'select all' in the message body, copy, and then paste into Excel, it parses into the worksheet's cells perfectly.)

Ideally I'd like the date/time stamp in Column-A then the header items in Columns B through H, with the data rows from the table following in Columns B through H and the date/time stamp repeating for each of the data rows. However, I'll take it as I can get it. Simply putting the timestamp in a cell, copy/pasting the body content below, and then repeating for all the emails would be perfectly fine. The data provided in the "Audit" worksheet created in the code below is nice, but not required.

Whether it is tweaking the code below or something entirely new, I don't care. I will greatly appreciate any help with the task.


Thanks in advance,
Andrew


Code:
Option Explicit
Public gblStopProcessing As Boolean
Sub ReadOutlookMessagesBody()
' requires Microsoft Outlook Object Library (Tools/References}
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAudit As Worksheet
Dim wsControl As Worksheet
Dim objFolder As Object
Dim objMsg As Object
Dim objNSpace As Object
'
Dim objOutlook As Outlook.Application
'
Dim blRecordsHaveFormulas As Boolean
Dim blColourCell As Boolean
'
Dim Item As Variant 'MailItem
Dim Lines() As String
Dim xlRow As Long
Dim Keys
Dim xlSheet As Object 'Excel.Worksheet
Dim I As Long, J As Long, P As Long
'
Dim strDelimiter As String
Dim strText As String
Dim lngAuditRecord As Long
Dim lngKount As Long
Dim lngRecordsInEmail As Long
Dim lngRow As Long
Dim lngSaveType As Long ' olTXT save type
Dim lngTotalItems As Long
Dim lngTotalRecords As Long
Dim intFormulasEndColumn As Integer
Dim intFormulasStartColumn As Integer
Dim intFreefile As Integer
Dim intKount As Integer
'
 On Error GoTo HandleError
'
    ' Initialise:
     Set wb = ThisWorkbook
     Set wsControl = wb.Worksheets("Control Sheet")
     strDelimiter = wsControl.Range("B3").Value
     blColourCell = False
     lngAuditRecord = 1 ' Start row
     lngSaveType = 0 ' save as .txt
     lngTotalRecords = 0
'
    ' Read email messages:
     Application.ScreenUpdating = False
     Set objOutlook = CreateObject("Outlook.Application")
     Set objNSpace = objOutlook.GetNamespace("MAPI")
'
    ' Allow user to choose folder:#
     Set objFolder = objNSpace.pickfolder
    ' Check if cancelled:
     If objFolder Is Nothing Then
         gblStopProcessing = True
         MsgBox "Processing cancelled"
        Exit Sub
    End If
'
     lngTotalItems = objFolder.Items.Count
     If lngTotalItems = 0 Then
         MsgBox "Outlook folder contains no email messages", vbOKOnly + vbCritical, "Error - Empty Folder"
         gblStopProcessing = True
        GoTo HandleExit
    End If
     If lngTotalItems > 0 Then
         On Error Resume Next
             Application.DisplayAlerts = False
             wb.Worksheets("Merge Data").Delete
             wb.Worksheets("Audit").Delete
             Application.DisplayAlerts = True
         On Error GoTo HandleError
         wb.Worksheets.Add after:=Worksheets(Worksheets.Count)
         Set ws = ActiveSheet
         ws.Name = "Merge Data"
         wb.Worksheets.Add after:=Worksheets(Worksheets.Count)
         Set wsAudit = ActiveSheet
         wsAudit.Name = "Audit"
         wsAudit.Range("A1") = "Email data imported on " & Now()
         lngAuditRecord = lngAuditRecord + 1
         wsAudit.Cells(lngAuditRecord, 1) = "Subject"
         wsAudit.Cells(lngAuditRecord, 2) = "Sender's Email Address"
         wsAudit.Cells(lngAuditRecord, 3) = "Email Creation Time"
         wsAudit.Cells(lngAuditRecord, 4) = "Email Received Time"
         wsAudit.Cells(lngAuditRecord, 5) = "Records Imported"
         wsAudit.Range(Cells(lngAuditRecord, 1), Cells(lngAuditRecord, 1)).Select
         Selection.EntireRow.Font.Bold = True
         Selection.HorizontalAlignment = xlCenter
         lngAuditRecord = lngAuditRecord + 1
         ws.Activate
         lngRow = 1 ' start row in worksheet "Merge Data"
         For lngKount = 1 To lngTotalItems
         
         
    If Item.subject Like "Sessions Check - SRTACP01" Then
      'Get all lines from the mailbody
      Lines = Split(Item.Body, vbCrLf) 'I WOULD BE PERFECTLY FINE WITH A SIMPLY COPY/PASTE HERE.  I JUST DO NOT KNOW HOW TO DO IT.
      'Next line in excel sheet
      xlRow = xlRow + 1
      xlSheet.Cells(xlRow, UBound(Keys) + 2) = Item.subject
       'Visit all lines
      For I = 0 To UBound(Lines)
        'Search all keywords in each line
        For J = 0 To UBound(Keys)
          P = InStr(1, Lines(I), Keys(J), vbTextCompare)
          If P > 0 Then
            'Store the right part after the keyword
            xlSheet.Cells(xlRow, J + 1) = Trim$(Mid$(Lines(I), P + Len(Keys(J)) + 1))
            Exit For
          End If
        Next
      Next
    End If
         
         
         
         
            ' switch cell colouring:             THIS IS HANDY, BUT NOT AT ALL REQUIRED
             blColourCell = Not blColourCell
'
' Update Audit record:                          THIS IS NICE, BUT NOT AT ALL REQUIRED
             On Error Resume Next
             wsAudit.Activate
             wsAudit.Cells(lngAuditRecord, 1) = objFolder.Items(lngKount).subject
             wsAudit.Cells(lngAuditRecord, 2) = objFolder.Items(lngKount).SenderEmailAddress
             wsAudit.Cells(lngAuditRecord, 3) = objFolder.Items(lngKount).CreationTime
             wsAudit.Cells(lngAuditRecord, 4) = objFolder.Items(lngKount).ReceivedTime
             wsAudit.Cells(lngAuditRecord, 5) = lngRecordsInEmail
             wsAudit.Range("A1").Select
             wsAudit.Cells.Columns.AutoFit
             lngAuditRecord = lngAuditRecord + 1
             On Error GoTo HandleError
'
             ws.Activate
        Next lngKount
         'Kill ThisWorkbook.Path & "\temp3210.txt"
   End If
'
' Check that records have been found:
     If lngTotalRecords = 0 Then
         MsgBox "No records were found for import", vbOKOnly + vbCritical, "Error - no records found"
         gblStopProcessing = True
        GoTo HandleExit
    End If

HandleExit:
     On Error Resume Next
     Application.ScreenUpdating = True
     Set objNSpace = Nothing
     Set objFolder = Nothing
     Set objMsg = Nothing
     Set objOutlook = Nothing
     Set ws = Nothing
     Set wsAudit = Nothing
     Set wsControl = Nothing
     Set wb = Nothing
'
     If Not gblStopProcessing Then
            MsgBox "Processing completed" & vbCrLf & vbCrLf & _
               "Please check results", vbOKOnly + vbInformation, "Information"
    End If
'
    Exit Sub
'
HandleError:
    MsgBox Err.Number & vbCrLf & Err.Description
    gblStopProcessing = True
    Resume HandleExit
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Okay, I've been able to modify the code to what is below. It works fine and does almost what I need. For each email, it places the date/time stamp in Column-A and then the body of the email in Column-B. However there are multiple lines within each cell in Column-B. I now need to create a new worksheet with one row for each line in Column-B and the date/time stamp associated to it in the next column.

Can someone help me with the new task?

Thanks in advance,
Andrew

This code gets me almost there:
Code:
Option Explicit
Public gblStopProcessing As Boolean
Sub ParseBlockingSessionsEmail()
' requires Microsoft Outlook Object Library (Menu: Tools/References}
Dim wb As Workbook
Dim ws As Worksheet
Dim objFolder As Object
Dim objNSpace As Object
Dim objOutlook As Outlook.Application
Dim lngAuditRecord As Long
Dim lngCount As Long
Dim lngTotalItems As Long 'Count of emails in the Outlook folder.
Dim lngTotalRecords As Long
Dim i As Integer
Dim EmailCount As Integer 'The counter, which starts at zero.
'
 On Error GoTo HandleError
'
    ' Initialize:
     Set wb = ThisWorkbook
     lngAuditRecord = 1 ' Start row
     lngTotalRecords = 0
'
    ' Read email messages:
     Application.ScreenUpdating = False
     Set objOutlook = CreateObject("Outlook.Application")
     Set objNSpace = objOutlook.GetNamespace("MAPI")
'
    ' Allow user to choose folder:#
     Set objFolder = objNSpace.pickfolder
    ' Check if cancelled:
     If objFolder Is Nothing Then
         gblStopProcessing = True
         MsgBox "Processing cancelled"
        Exit Sub
     End If
'
     lngTotalItems = objFolder.Items.Count
     If lngTotalItems = 0 Then
         MsgBox "Outlook folder contains no email messages", vbOKOnly + vbCritical, "Error - Empty Folder"
         gblStopProcessing = True
        GoTo HandleExit
     End If
    If lngTotalItems > 0 Then
         On Error Resume Next
             Application.DisplayAlerts = False
             wb.Worksheets("Merge Data").Delete
             wb.Worksheets("Audit").Delete
             Application.DisplayAlerts = True
         On Error GoTo HandleError
         wb.Worksheets.Add after:=Worksheets(Worksheets.Count)
         Set ws = ActiveSheet
         ws.Name = "Merge Data"
         
        'Insert Title Row and Format
         ws.Cells(1, 1) = "Received"
         ws.Cells(1, 2) = "Email Body"
         'ws.Cells(lngAuditRecord, 3) = "Subject"
         'ws.Cells(lngAuditRecord, 4) = "Attachments Count"
         'ws.Cells(lngAuditRecord, 4) = "Sender Name"
         'ws.Cells(lngAuditRecord, 5) = "Sender Email"
         ws.Range(Cells(lngAuditRecord, 1), Cells(lngAuditRecord, 1)).Select
         Selection.EntireRow.Font.Bold = True
         Selection.HorizontalAlignment = xlCenter
         
         'Populate the workbook
         For lngCount = 1 To lngTotalItems
             Application.StatusBar = "Reading message " & lngCount & " of " & lngTotalItems
                i = 0
                'read email info
                While i < lngTotalItems
                    i = i + 1
                    If i Mod 50 = 0 Then Application.StatusBar = "Reading email messages " & Format(i / lngTotalItems, "0%") & "..."
                    With objFolder.Items(i)
                        Cells(i + 1, 1).Formula = .ReceivedTime
                        Cells(i + 1, 2).Formula = .Body
                        'Cells(i + 1, 3).Formula = .subject
                        'Cells(i + 1, 4).Formula = .Attachments.Count
                        'Cells(i + 1, 5).Formula = .SenderName
                        'Cells(i + 1, 6).Formula = .SenderEmailAddress
                    End With
                Wend
                'Set objFolder = Nothing
             ws.Activate
         Next lngCount
         lngTotalRecords = lngCount
        
        'Format Worksheet
            Columns("B:B").Select
            Selection.ColumnWidth = 255
            Cells.Select
            Selection.Columns.AutoFit
            Selection.Rows.AutoFit
            With Selection
                .VerticalAlignment = xlTop
            End With
            Range("A1").Select
    End If
'
' Check that records have been found:
     If lngTotalRecords = 0 Then
         MsgBox "No records were found for import", vbOKOnly + vbCritical, "Error - no records found"
         gblStopProcessing = True
         GoTo HandleExit
    End If
'
HandleExit:
     On Error Resume Next
     Application.ScreenUpdating = True
     Set objNSpace = Nothing
     Set objFolder = Nothing
     Set objOutlook = Nothing
     Set ws = Nothing
     Set wb = Nothing
     If Not gblStopProcessing Then
            MsgBox "Processing completed" & vbCrLf & vbCrLf & _
               "Please check results", vbOKOnly + vbInformation, "Information"
     End If
     Exit Sub
'
HandleError:
    MsgBox Err.Number & vbCrLf & Err.Description
    gblStopProcessing = True
    Resume HandleExit
End Sub
 
Upvote 0
Update: I've noted that each line of data in the Column-B cells ends with the character 'code(10)'.
 
Upvote 0
Figured it out myself. It's not glamorous, but it works. I kind of kept a list of the resources I used. They are shown at the bottom.
-----------------------------------------------------------

Code:
Option Explicit
Public gblStopProcessing As Boolean
Sub ParseBlockingSessionsEmailPartOne()
' This macro requires Microsoft Outlook Object Library (Menu: Tools/References) be available
Dim wb As Workbook
Dim ws As Worksheet
Dim objFolder As Object
Dim objNSpace As Object
Dim objOutlook As Outlook.Application
Dim lngAuditRecord As Long
Dim lngCount As Long
Dim lngTotalItems As Long 'Count of emails in the Outlook folder.
Dim lngTotalRecords As Long
Dim i As Integer
Dim EmailCount As Integer 'The counter, which starts at zero.
'
On Error GoTo HandleError
'Application.ScreenUpdating = True
'Application.ScreenUpdating = False
'
Sheets("Merge Data").Select
'
' Initialize:
Set wb = ThisWorkbook
lngAuditRecord = 1 ' Start row
lngTotalRecords = 0
'
' Read email messages:
Application.ScreenUpdating = False
Set objOutlook = CreateObject("Outlook.Application")
Set objNSpace = objOutlook.GetNamespace("MAPI")
'
' Allow user to choose folder:#
Set objFolder = objNSpace.pickfolder
' Check if cancelled:
If objFolder Is Nothing Then
gblStopProcessing = True
MsgBox "Processing cancelled"
Exit Sub
End If
'
lngTotalItems = objFolder.Items.Count
If lngTotalItems = 0 Then
MsgBox "Outlook folder contains no email messages", vbOKOnly + vbCritical, "Error - Empty Folder"
gblStopProcessing = True
GoTo HandleExit
End If
If lngTotalItems > 0 Then
On Error Resume Next
Application.DisplayAlerts = False
wb.Worksheets("Merge Data").Delete
'wb.Worksheets("Audit").Delete
Application.DisplayAlerts = True
On Error GoTo HandleError
wb.Worksheets.Add After:=Worksheets(Worksheets.Count)
Set ws = ActiveSheet
ws.Name = "Merge Data"

'Insert Title Row and Format NOTE: THE MACRO CAN BE USED TO PARSE OUT OTHER PARTS OF AN EMAIL.
' I JUST COMMENTED OUT THE LINES NOT USED FOR THE CURRENT PROJECT.
ws.Cells(1, 1) = "Received"
ws.Cells(1, 2) = "Email Body"
'ws.Cells(lngAuditRecord, 3) = "Subject"
'ws.Cells(lngAuditRecord, 4) = "Attachments Count"
'ws.Cells(lngAuditRecord, 4) = "Sender Name"
'ws.Cells(lngAuditRecord, 5) = "Sender Email"
ws.Range(Cells(lngAuditRecord, 1), Cells(lngAuditRecord, 1)).Select
Selection.EntireRow.Font.Bold = True
Selection.HorizontalAlignment = xlCenter

'Populate the workbook
For lngCount = 1 To lngTotalItems
Application.StatusBar = "Reading message " & lngCount & " of " & lngTotalItems
i = 0
'read email info
While i < lngTotalItems
i = i + 1
If i Mod 50 = 0 Then Application.StatusBar = "Reading email messages " & Format(i / lngTotalItems, "0%") & "..."
With objFolder.Items(i)
Cells(i + 1, 1).Formula = .ReceivedTime
Cells(i + 1, 2).Formula = .Body
'Cells(i + 1, 3).Formula = .subject
'Cells(i + 1, 4).Formula = .Attachments.Count
'Cells(i + 1, 5).Formula = .SenderName
'Cells(i + 1, 6).Formula = .SenderEmailAddress
End With
Wend
'Set objFolder = Nothing
ws.Activate
Next lngCount
lngTotalRecords = lngCount

'Format Worksheet
Columns("B:B").Select
Selection.ColumnWidth = 255
Cells.Select
Selection.Columns.AutoFit
Selection.Rows.AutoFit
With Selection
.VerticalAlignment = xlTop
End With
Range("A1").Select
End If
'
' Check that records have been found:
If lngTotalRecords = 0 Then
MsgBox "No records were found for import", vbOKOnly + vbCritical, "Error - no records found"
gblStopProcessing = True
GoTo HandleExit
End If
'
With Selection
Cells.Select
.VerticalAlignment = xlTop
.WrapText = True
End With
Range("A1").Select
'
HandleExit:
On Error Resume Next
Application.ScreenUpdating = True
Set objNSpace = Nothing
Set objFolder = Nothing
Set objOutlook = Nothing
Set ws = Nothing
Set wb = Nothing
If Not gblStopProcessing Then
MsgBox "Processing completed" & vbCrLf & vbCrLf & _
"Please check results", vbOKOnly + vbInformation, "Information"
End If
Call ParseBlockingSessionsEmailPartTwo
Exit Sub
'
HandleError:
MsgBox Err.Number & vbCrLf & Err.Description
gblStopProcessing = True
Resume HandleExit
End Sub
'==================================================================================
Sub ParseBlockingSessionsEmailPartTwo()
Dim vPrevChar10 As Integer
Dim vNextChar10 As Integer
Dim vCounter As Integer
Dim vLastEmail As Long
Dim vEmailBody As String
Dim vRowsInEmail As Integer
Dim vRecordCounter As Integer
Application.ScreenUpdating = True
Application.StatusBar = "Emails imported. Working on parsing the data therein."
Application.ScreenUpdating = False
Sheets("Merge Data").Select
Columns("B:B").Select 'Following gets rid of the email body content that comes prior to the data table
Selection.Replace What:="Blocking Sessions Check" & Chr(32) & Chr(13) & Chr(10) & Chr(13) & Chr(10) & Chr(13) & Chr(10), Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
vCounter = 1
vNextChar10 = 0
vRecordCounter = 1

With ActiveSheet
vLastEmail = .Cells(.Rows.Count, "B").End(xlUp).Row 'Last row in Column-B
End With

For vRecordCounter = 2 To vLastEmail 'Performs internal operation, one row per email, starting at B2.
Range("B" & vRecordCounter).Select
vEmailBody = ActiveCell.Value
vRowsInEmail = Len(vEmailBody) - Len(Replace(vEmailBody, Chr(10), "")) + 1 'Counts the number of line returns in the email body and adds one to get the rows in the body
With Range("B" & vRecordCounter & ":B" & vRecordCounter)
For vCounter = 1 To vRowsInEmail 'Will add a column to each row for each line in the email body
vPrevChar10 = vNextChar10 + 1
vNextChar10 = InStr(vPrevChar10, ActiveCell.Value, Chr(10))
.Offset(, vCounter + 1) = "=IFERROR(MID($B" & vRecordCounter & "," & vPrevChar10 & "," & vNextChar10 - vPrevChar10 + 0 & "), RIGHT($B" & vRecordCounter & ", LEN($B" & vRecordCounter & ") - " & vPrevChar10 - 1 & "))"
Next
End With
Next
Call ParseBlockingSessionsEmailPartThree
End Sub
'==================================================================================
Sub ParseBlockingSessionsEmailPartThree()
Dim vmdLastEmail As Long
Dim vmdRow As Integer
Dim vdRowA As Integer
Dim vdRowB As Integer
Application.ScreenUpdating = True
Application.StatusBar = "Emails imported. Data parsed. Working on formatting the data."
Application.ScreenUpdating = False
vmdRow = 2
'While in Merge Data worksheet
Sheets("Merge Data").Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Range("A2").Select
With ActiveSheet
vmdLastEmail = .Cells(.Rows.Count, "B").End(xlUp).Row 'Last row in Column-B of Merge Data worksheet
End With

'While in Merge Data worksheet
For vmdRow = 2 To vmdLastEmail
Sheets("Merge Data").Select
Range("B" & vmdRow).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
'While in Data worksheet
Sheets("Data").Select
Range("A1").Select
With ActiveSheet
vdRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'Last row in Column-A of Merge Data worksheet
End With
Range("B1").Select
With ActiveSheet
vdRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'Last row in Column-B of Merge Data worksheet
End With
Range("B" & vdRowB).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Application.CutCopyMode = False
With ActiveSheet
vdRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'NEW last row in Column-B
End With
'Back to Merge Data worksheet
Sheets("Merge Data").Select
Range("A" & vmdRow).Select
Selection.Copy
'Back to Data worksheet
Sheets("Data").Select
Range("A" & vdRowA & ":A" & vdRowB).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Next
Range("A2").Select

'Wrap it up
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "EMAIL_DATE-TIME"
Range("B1").Select
ActiveCell.FormulaR1C1 = "BLOCKING-SESSION-RECORD"
Range("A1:B1").Select
Selection.Font.Bold = True
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
Application.StatusBar = "All done. Emails imported, data parsed and formatted (a little)."
MsgBox "All done. Have fun."
ActiveWindow.FreezePanes = True
Cells.Select
Selection.Columns.AutoFit
Range("C1").Select
End Sub
'==============================================================================================================================================================
'SOME RESOURCES, in no particular order. Some worked, some didn't, but kept the list as a reference in case needed.
'http://www.mrexcel.com/forum/excel-questions/345240-export-outlook-excel.html
'http://answers.microsoft.com/en-us/office/forum/office_2007-customize/macro-to-extract-outlook-message-body-into-excel/ff10a749-07ea-4394-9721-7cb83eaacc0e
'http://www.ozgrid.com/forum/showthread.php?t=181512
'http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=622
'http://peltiertech.com/
'http://www.vbaexpress.com/forum/showthread.php?39537-SelectAll-in-VBA-or-copy-the-body
'http://www.mrexcel.com/forum/excel-questions/739381-outlook-email-content-excel-visual-basic-applications.html#post3634126
'http://answers.microsoft.com/en-us/office/forum/office_2007-excel/copy-email-body-to-excel-vba/b1561bb5-f5c1-4fe2-967e-cc1bb360420b?tm=1384385675666
 
Upvote 0
You could also speed it up a bit by removing the Select.Selection statements in all macros....see below
Also, consider using .UsedRange rather than .Cells
Code:
Sub ParseBlockingSessionsEmailPartThree()
Dim vmdLastEmail As Long, vmdRow As Integer, vdRowA As Integer, vdRowB As Integer
Application.ScreenUpdating = True
Application.StatusBar = "Emails imported. Data parsed. Working on formatting the data."
Application.ScreenUpdating = False
vmdRow = 2
'While in Merge Data worksheet
With Sheets("Merge Data").UsedRange
.Value = .Value
End With
Columns("B:C").Delete
With ActiveSheet
vmdLastEmail = .Cells(.Rows.Count, "B").End(xlUp).Row 'Last row in Column-B of Merge Data worksheet
End With

'While in Merge Data worksheet
For vmdRow = 2 To vmdLastEmail
Sheets("Merge Data").Range("B" & vmdRow).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
'While in Data worksheet
With Sheets("Data")
vdRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'Last row in Column-A of Merge Data worksheet
vdRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'Last row in Column-B of Merge Data worksheet
End With
Range("B" & vdRowB).Select
Selection.PasteSpecial Paste:=xlPasteAll
With Sheets("Data")
vdRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'NEW last row in Column-B
End With
'Back to Merge Data worksheet
Sheets("Merge Data").Range("A" & vmdRow).Copy Destination:=Sheets("Data").Range("A" & vdRowA & ":A" & vdRowB)
Next

'Wrap it up
Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Value = "EMAIL_DATE-TIME"
Range("B1").Value = "BLOCKING-SESSION-RECORD"
Range("A1:B1").Font.Bold = True
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
Application.StatusBar = "All done. Emails imported, data parsed and formatted (a little)."
MsgBox "All done. Have fun."
ActiveWindow.FreezePanes = True
Cells.Columns.AutoFit
End Sub
 
Upvote 0
Ah, thank you. If I need to re-run the macro to gather more data then I'll make the suggested tweaks.

Thanks again,
Andrew
 
Upvote 0

Forum statistics

Threads
1,223,964
Messages
6,175,657
Members
452,664
Latest member
alpserbetli

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