workbook close not working

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,004
Office Version
  1. 365
Platform
  1. Windows
folks, I have a macro that , so far, opens another workbook, copies a certain tab into thisworkbook, and now I am trying to close the second workbook. when I run the macro, I get a "subscript out of range" on the Workbooks(MLPwkb).Close False line.

here is my code:

Code:
Option Explicit
Sub reportpackage()
    Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim MLPwkb As String, SourceWb As Workbook
    
    Set SourceWb = ThisWorkbook
   
    MyPath = \\FPMB1FNP02\Groups$\Internal_Management_Reports\DailyMLP
      
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        
    MyFile = Dir(MyPath & "*.xls", 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
    
    MLPwkb = MyPath & LatestFile
   With Workbooks.Open(MLPwkb)
  Application.DisplayAlerts = False
       
       ' .Sheets("MLP").UsedRange.Copy
    
  End With


With SourceWb
.Sheets("MLP").Range("A1").PasteSpecial (xlPasteValues)
.Sheets("Dashboard").Range("N2") = LatestDate
End With

Workbooks(MLPwkb).Close False
End Sub


Also, I can't seem to suppress the Microsoft Security Notice to enable or disable macros for the file I am opening. How is this done?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
The index for the Workbooks object requires the workbook name only, and should not include the path, try....

Code:
Workbooks([COLOR=#574123]LatestFile[/COLOR]).Close False
 
  • Like
Reactions: ajm
Upvote 0
The index for the Workbooks object requires the workbook name only, and should not include the path, try....

Code:
Workbooks([COLOR=#574123]LatestFile[/COLOR]).Close False

Domenic, thank you.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

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