yinkajewole
Active Member
- Joined
- Nov 23, 2018
- Messages
- 281
using a listbox, how do i let it display up to 60 recently opened files.
@yinkajewole - perhaps you did not see my solution in post#4
@My Aswer Is This confirmed that the solution posted works well
Q1 Did you test it?
Q2 Why would this be helpful? Are you wanting a longer list than Excel maintains?
@yinkajewole - perhaps you did not see my solution in post#4
@My Aswer Is This confirmed that the solution posted works well
Q1 Did you test it?
Q2 Why would this be helpful? Are you wanting a longer list than Excel maintains?
- I have a few ideas that I will test tomorrow
Sub Latest_60_Files()
Dim r As Long
'update list in sheet
Rows("1:50").Insert
For r = 1 To 50
Range("A" & r) = Application.RecentFiles(r).Path
Next
'remove duplicates, empty cells & old entries
Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
On Error Resume Next
Range("A1").Resize(120).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Range("A1").Offset(60).Resize(60).ClearContents
'save workbook
ThisWorkbook.Save
End Sub
ListBox1.Clear
Dim x As Integer
For r = 1 To 60
ListBox1.AddItem Range("A" & r ).Value
Next r
Workbooks.Open (ListBox1.Value)
On Error Resume Next
Workbooks.Open (ListBox1.Value)
If Err.Number > 0 Then MsgBox "File not found"
On Error GotTo 0
Use a NEW workbook
- VBA below maintains a list of "recent" files in column A of the active sheet
- expect the list to contain 50 files the first time
- run again after working on other files
- list increases to a maximum of 60 "recent" files (as requested in post#1)
Code:Sub Latest_60_Files() Dim r As Long 'update list in sheet Rows("1:50").Insert For r = 1 To 50 Range("A" & r) = Application.RecentFiles(r).Path Next 'remove duplicates, empty cells & old entries Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo On Error Resume Next Range("A1").Resize(120).SpecialCells(xlCellTypeBlanks).EntireRow.Delete On Error GoTo 0 Range("A1").Offset(60).Resize(60).ClearContents 'save workbook ThisWorkbook.Save End Sub
The above code should provide the correct values to populate your listbox
Some sample ListBox code which may help you:
Remove prior entries in ListBox with
Code:ListBox1.Clear
To add items to a ListBox (as previously demonsted in post#4)
Code:Dim x As Integer For r = 1 To 60 ListBox1.AddItem Range("A" & r ).Value Next r
To open a workbook from the ListBox (perhaps using Private Sub ListBox1_Change)
Code:Workbooks.Open (ListBox1.Value)
Remember to add controls to prevent VBA failing
"Recent" file list in Excel is NOT updated when a file is deleted or moved
So perhaps something along these lines
Code:On Error Resume Next Workbooks.Open (ListBox1.Value) If Err.Number > 0 Then MsgBox "File not found" On Error GotTo 0
Private Sub Workbook_Open()
Application.EnableEvents = False
Sheet1.Range("B1").Select
Application.EnableEvents = True
Call Sheet1.Latest_60_Files
MsgBox "Click on empty cell to close workbook" & vbcr & "or file name to open file"
End Sub
Sub Latest_60_Files()
Dim r As Long
'update list in sheet
Rows("1:50").Insert
For r = 1 To 50
Range("A" & r) = Application.RecentFiles(r).path
Next
'remove duplicates, empty cells & old entries
Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
On Error Resume Next
Range("A1").Resize(120).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Range("A1").Offset(60).Resize(60).ClearContents
'save workbook
ThisWorkbook.Save
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
On Error Resume Next
Workbooks.Open (Target)
If Err.Number > 0 Then MsgBox "File not found"
On Error GoTo 0
ThisWorkbook.Close False
End Sub