How to Close file after pasting info from it is complete

jason1000rr

New Member
Joined
Sep 2, 2016
Messages
14
Hi Everyone
Looking for some help to close the workbook after code is executed. It's a bit lengthy, so if you see some room for improvement as well, please do tell

Code:
Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim ws As Worksheet
    Sheets("IQR").Select
    Columns("A:AL").Select
    Selection.ClearContents
    Range("A1").Select
    MyPath = "\\global.corp.net\site\Procurement\IQR"
    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
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*20*" Then ws.Select
    Next ws
    ActiveSheet.Cells.Copy
    For Each wb In Application.Workbooks
    If wb.Name Like "*Kanban Calculator*" Then wb.Activate
    Next wb
    Range("A1").Select
    ActiveSheet.Paste
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is this what you need?
Workbooks(LatestFile).Close False
This should close 'LatestFile' without saving it.
 
Upvote 0
@jason1000rr
I can't escape the impression that you ask similar questions in several threads. How a recently opened workbook can be closed (your additional question in a recent previous thread ) has already been answered in post # 6.
 
Upvote 0
@jason1000rr
I can't escape the impression that you ask similar questions in several threads. How a recently opened workbook can be closed (your additional question in a recent previous thread ) has already been answered in post # 6.
I know you helped on the other thread, however, I do not see this impression. No need to go into detail. All I would ask from all members is not to be too quick at conclusions. Let's all help each other in order to keep members. Again, thank you for your previous help.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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