workbook close not working

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,053
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,223,626
Messages
6,173,415
Members
452,514
Latest member
cjkelly15

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