title: Combo box listing latest 2 folders from path
body: <p>I am quite new to VBA as I have been using other programming languages. </p>
  
  <p>I am trying to use a combo box to list the latest 2 folders from my path. </p>
  
  <p>I have already been able to select all data from the required path as well as I have sorted this. </p>
  
  <p>I need support to be able to list only the latest 2 folders based on my code but am struggling and require help. </p>
  
  <pre><code>Drivepath = Mid(ThisWorkbook.Path, 1, 2)
  On Error Resume Next
  filepath = Drivepath &amp; "C:\Users\Documents\Month\" &amp; ThisWorkbook.Sheets("Months"). ComboBox1.Value &amp; "\"
  
  Application.Workbooks.Open (filepath &amp; s_workbook)
  
  Application.Sheets(1).Activate
  
  
  Dim name
  ThisWorkbook.Sheets("Months"). ComboBox1.Clear
  Drivepath = Mid(ThisWorkbook.Path, 1,2)
  
  For Each name In ListDirectory(Path:=Drivepath &amp; "C:\Users\Documents\Month”, AttrInclude:=vbDirectory, AttrExclude:=vbSystem Or vbHidden)
      If Len(name) &gt; 4 Then
          If InStr(name, "list") = 0 Then ThisWorkbook.Sheets("Months"). ComboBox1.AddItem name
      End If
  Next name
  
  'Sort the list
  
  ComBoList = ThisWorkbook.Sheets("Months"). ComboBox1.List
  
  For X = LBound(ComBoList) To UBound(ComBoList) - 1
      For j = X + 1 To UBound(ComBoList)
          If ComBoList(X, 0) &gt; ComBoList(j, 0) Then
              ComBoTemp = ComBoList(X, 0)
              ComBoList(X, 0) = ComBoList(j, 0)
              ComBoList(j, 0) = ComBoTemp
          End If
      Next j
  Next X
  
  hold_name = ComBoList(UBound(ComBoList), 0)
  
  ThisWorkbook.Sheets("Months"). ComboBox1.List = ComBoList
  ThisWorkbook.Sheets("Months"). ComboBox1.Value = hold_name
  
  ListDirectory  
  </code></pre>
  
  <p><code>ListDirectory</code> function</p>
  
  <pre><code>Function ListDirectory(Path As String, AttrInclude As VbFileAttribute, Optional AttrExclude As VbFileAttribute = False) As Collection
  
      Dim Filename As String
  
      Dim Attribs As VbFileAttribute
      Set ListDirectory = New Collection
      ' first call to Dir() initializes the list
  
      Filename = Dir(Path, AttrInclude)
      While Filename &lt;&gt; ""
          Attribs = GetAttr(Path &amp; Filename)
  
          ' to be added, a file must have the right set of attributes
  
          If Attribs And AttrInclude And Not (Attribs And AttrExclude) Then
  
              If Len(Filename) &gt; 4 And InStr(Filename, "Oracle") = 0 Then 
                  ListDirectory.Add Filename, Path &amp; Filename
              End If
  
          ' fetch next filename
  
          Filename = Dir
  
      Wend
  
  End Function
  </code></pre>
  
  <p>Am using sorting to sort the folders as all the required folders are named the following. E.g, 201901, 201902, 201903, 201904, 201905 and etc.</p>
  
  <p>I just need a solution for selecting the last 2 folders which are 202003 &amp; 202004. </p>
  
  <p>I could easily delete all the other folders from the path but am looking for a more efficient way to only display 2 of the latest folders in the combo box. </p>
  
  <p>Again, I have already sorted them but once I sorted them I would like to display or select only the latest folders based on sorting them.</p>