Open file based on its filename date and copy its content

tkc123

New Member
Joined
Oct 12, 2018
Messages
6
Hi everyone,

I've been trying to create an Active X button to open a file based on the latest date on the filename and copy (the content) from its worksheet to my current workbook.

Inspiration drew from:
https://www.mrexcel.com/forum/excel...el-file-based-latest-date-found-filename.html

But Excel keeps crashing on every other run.
Could someone please check if the code is properly written?

Thank you so much in advance.

Code:
Private Sub CommandButton1_Click()    
    Dim dtTestDate As Date
    Dim sStartWB As String
    Dim x As Workbook
    
    
    Const sPath As String = "C:\Folder\"
    Const dtEarliest = #1/1/2018# 

    dtTestDate = Date
    sStartWB = ActiveWorkbook.Name
    
    While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
        On Error Resume Next
        Set x = Workbooks.Open(sPath & "Record - " & Format(dtTestDate, "YYYY-MM") & ".xlsx")
        dtTestDate = dtTestDate - 1
        On Error GoTo 0


    Wend




    If ActiveWorkbook.Name = sStartWB Then MsgBox "Earlier file not found."


        x.Sheets("Layout").Range("A1:B100").Copy
        ThisWorkbook.Sheets("Sheet1").Range("A1:B100").PasteSpecial
        
    'Close x:
    x.Close
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try to put a breakpoint in the begining of your code and run it to see where exactly is the problem.
 
Upvote 0
Apparently, the problem comes from clicking the command button.
I didn't encounter any issues by running the code directly in Visual Basic Editor, but it crashes when I click on the command button.

And how to prevent the macro from opening earlier files if the file with the latest date is already opened?
 
Upvote 0

Forum statistics

Threads
1,223,983
Messages
6,175,778
Members
452,668
Latest member
mrider123

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