title: Need to set a 'lookback x' days in VB Macro that pulls data from Outlook to Excel
body: <p><strong>What I am attempting:</strong> I am trying to extract emails from a custom subfolder. The emails contain a list of employees that we hire or terminate. The email fires daily. Every month we do an audit in which we have to go through each email and manually copy and paste the userIDs. I created a macro to extract all the emails in the subfolder but I cannot figure out how to filter to look back 45 days versus extract all the emails from the box.</p>
  <p><strong>Issue</strong>: I am trying to add a filter to look back 45 days only. I don't need the entire box to be extracted just the last 45 days from the day I run the macro. I have googled and couldn't figure it out.</p>
  <pre><code>
  
  Range(&quot;A2:H30000&quot;).Clear
  Dim OLApp As Outlook.Application
  Set OLApp = New Outlook.Application
  
  
  Dim ONS As Outlook.Namespace
  
  Set ONS = OLApp.GetNamespace(&quot;MAPI&quot;)
  Dim MYFOLDER As Outlook.Folder
   
  Set MYFOLDER = ONS.Folders(&quot;fakeemail@fakeemail.com&quot;).Folders(&quot;Inbox&quot;)
  Set MYFOLDER = MYFOLDER.Folders(&quot;NewHires&quot;)
  
  Dim OLMAIL As Outlook.MailItem
  Set OLMAIL = OLApp.CreateItem(olMailItem)
  
  Set myItems = MYFOLDER.Items
  myItems.Sort &quot;ReceivedTime&quot;, True
  
  
  For Each OLMAIL In myItems
   
  Dim oHTML As MSHTML.HTMLDocument
  Set oHTML = New MSHTML.HTMLDocument
   
    
  
  Dim oElColl As MSHTML.IHTMLElementCollection
  With oHTML
  .Body.innerHTML = OLMAIL.HTMLBody
  Set oElColl = .getElementsByTagName(&quot;table&quot;)
  End With
   
  Dim t As Long, r As Long, c As Long
  Dim eRow As Long
  
  For t = 0 To oElColl.Length - 1
      eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
      For r = 0 To (oElColl(t).Rows.Length - 1)
          For c = 0 To (oElColl(t).Rows(r).Cells.Length - 1)
              Range(&quot;A&quot; &amp; eRow).Offset(r, c).Value = oElColl(t).Rows(r).Cells(c).innerText
          Next c
      Next r
      eRow = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
      
      
  Next t
   
   
  Cells(eRow, 1) = &quot;Sender's Name:&quot; &amp; &quot; &quot; &amp; OLMAIL.Sender
  Cells(eRow, 1).Interior.Color = vbRed
  Cells(eRow, 1).Font.Color = vbWhite
  Cells(eRow, 2) = OLMAIL.ReceivedTime
  Cells(eRow, 2).Interior.Color = vbBlue
  Cells(eRow, 2).Font.Color = vbWhite
  Range(Cells(eRow, 1), Cells(eRow, 2)).Columns.AutoFit
  Next OLMAIL
  
  
  Range(&quot;A2&quot;).Select
  
  Set OLApp = Nothing
  Set OLMAIL = Nothing
  Set oHTML = Nothing
  Set oElColl = Nothing
  
  ThisWorkbook.VBProject.VBE.MainWindow.Visible = False
  
  End Sub
  </code></pre>