Open most recent file in folder, unmerge, copy, go to different file, paste. Need code, please help

Greg_ismyusername

New Member
Joined
Feb 10, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Would be very grateful to anyone who can provide assistance.

I use a program that exports data to an xlsx file in a particular directory.

The code below will open the most recent file in the hardcoded directory.


Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\gregs\Dropbox\Financials Export"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub


From here, I would like the macro to navigate to sheet "Cash Flow" and unmerge all cells, select all, copy.

The macro will be stored in a separate workbook called "Macros_Financial Analysis"

in '[Macros_Financial Analysis.xlsm]Sheet1'!$A$1 will be the path and filename of the paste destination.

Example C:\Users\gregs\Dropbox\123 Main Street\Financials\123 Main Street Financials_v##.xlsx

123 Main Street Financials_v##.xlsx will have a sheet named Cash Flow, which is the paste destination.

So basically, the macro, triggered from my macro workbook will open most recent file in particular directory, unmerge, copy, open a workbook in a different directory as specified by a cell in my macro workbook and then paste.

Beyond my ability and would sincerely appreciate some help.

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Would be very grateful to anyone who can provide assistance.

I use a program that exports data to an xlsx file in a particular directory.

The code below will open the most recent file in the hardcoded directory.


Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\gregs\Dropbox\Financials Export"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub


From here, I would like the macro to navigate to sheet "Cash Flow" and unmerge all cells, select all, copy.

The macro will be stored in a separate workbook called "Macros_Financial Analysis"

in '[Macros_Financial Analysis.xlsm]Sheet1'!$A$1 will be the path and filename of the paste destination.

Example C:\Users\gregs\Dropbox\123 Main Street\Financials\123 Main Street Financials_v##.xlsx

123 Main Street Financials_v##.xlsx will have a sheet named Cash Flow, which is the paste destination.

So basically, the macro, triggered from my macro workbook will open most recent file in particular directory, unmerge, copy, open a workbook in a different directory as specified by a cell in my macro workbook and then paste.

Beyond my ability and would sincerely appreciate some help.

Thanks
sorry for the formatting :

VBA Code:
Sub NewestFile()

    Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    
    MyPath = "C:\Users\gregs\Dropbox\_Blatteis and Schnur\415 N. Crescent\Crescent Financials\"
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    MyFile = Dir(MyPath & "*.xlsx", vbNormal)
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
    Do While Len(MyFile) > 0
        LMD = FileDateTime(MyPath & MyFile)
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        MyFile = Dir
    Loop
    Workbooks.Open MyPath & LatestFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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