How to copy contents of open workbook to current workbook

mflow610

New Member
Joined
Sep 5, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,



I am very new to VBA in Excel and I am running into an issue. I have conjured up a code that searches a folder and finds the most recent file based on its name (i.e. MRKT_VALUE_20230905 named for 9/5/2023). My code then opens the workbook with that name, but I am unable to figure out how to copy the contents of this workbook, paste it in a current workbook under a sheet named "Market Value", and then close the opened workbook that the contents were copied from.



Any help would be very grateful! Here is what I have so far:



VBA Code:
Sub OpenLatest()
'---Opens a sheet based on date, searches backward from today til it finds a matching date

    Dim dtTestDate As Date
Dim sActiveWB As Workbook
Dim sCurrentWB As Excel.Worksheet
Dim sValue As String

    Const sPath As String = "J:\LSW_MARKET_INDEX_VALUES\"
Const dtEarliest = #1/1/2016#    '--to stop loop if file not found by earliest valid date

Set sCurrentWB = ThisWorkbook.Sheets("Market Values")

dtTestDate = Date
sActiveWB = ActiveWorkbook

While ActiveWorkbook.Name = sActiveWB And dtTestDate >= dtEarliest

        On Error Resume Next
Debug.Print "Trying to open: " & _
sPath & "MRKT_VALUE_" & Format(dtTestDate, "YYYYMMDD") & ".xls"
Workbooks.Open sPath & "MRKT_VALUE_" & Format(dtTestDate, "YYYYMMDD") & ".xls"
sValue = sActiveWB.Sheets(1).Range("A1").Value
ThisWorkbook.Sheets(1).Range("B11").Value = sValue
dtTestDate = dtTestDate - 1
On Error GoTo 0
Wend

 

    Application.DisplayAlerts = True

 

    If ActiveWorkbook.Name = sActiveWB Then MsgBox "Earlier file not found."
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Maybe something like this.
VBA Code:
Sub OpenLatest()
    Const sPath As String = "J:\LSW_MARKET_INDEX_VALUES\"

    Dim SrcWB As Workbook
    Dim FSO As Object, FFolder As Object, FFile As Object
    Dim FilePath As String
    Dim sCurrentWS As Worksheet
    Dim sValue As String, FileDate As String, LastFileDate As String, sTmp As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    If Not FSO.folderexists(sPath) Then
        MsgBox "Folder '" & sPath & "' does not exist"
        Exit Sub
    End If
    
    Set sCurrentWS = ThisWorkbook.Sheets("Market Values")
    Set FFolder = FSO.getfolder(sPath)
    
    Application.ScreenUpdating = False
    For Each FFile In FFolder.Files
        If Left(UCase(FFile.Name), 10) = "MRKT_VALUE" Then
            sTmp = Split(FFile.Name, ".")(0)
            FileDate = Right(sTmp, Len(sTmp) - InStrRev(sTmp, "_"))
            If IsNumeric(FileDate) And FileDate > LastFileDate Then
                LastFileDate = FileDate
            End If
        End If
    Next FFile
    
    If LastFileDate <> "" Then
        FilePath = sPath & "MRKT_VALUE_" & LastFileDate & ".xls"
        Set SrcWB = Workbooks.Open(FilePath)
        sValue = SrcWB.Sheets(1).Range("A1").Value
        sCurrentWS.Range("B11").Value = sValue
        SrcWB.Close False
        MsgBox "Done."
    Else
        MsgBox "Valid file not found"
    End If
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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