Close A File That Is Open Where You Only Know Part Of The Filename

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
231
Hi all,

I am trying to close a file using VBA which in itself is quite simple however the file name changes so I only ever know a part of the filename and its location.

Location:
P:\Public User Area\Purchasing\Suppliers\National Tube Stockholders\Price Review\

The section of the filename that I have is: "Price List - "

I would also like it to save with-out prompts as well.


Can anyone help me with the VBA code to do this?


Thanks in advance
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,857
Office Version
365
Platform
Windows
Is VBA opening the file?
If so, please post your code that opens it.
 

ParamRay

Well-known Member
Joined
Aug 6, 2014
Messages
1,195
If the file you're trying to close is an Excel workbook, and you're trying to close it using VBA in another (separate) Excel workbook, you could try the code below.

If this isn't what you're trying to do, please refer to the post above and give some more info.

Code:
Sub CloseWorkbook()
  Dim Wb As Workbook
  For Each Wb In Application.Workbooks
    If Wb.Name Like "Price List*" Then
      Wb.Close SaveChanges:=True ' or false to discard changes
      Exit Sub
    End If
  Next Wb
End Sub
 

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
231
Excellent this worked a treat thank you for your help, it's much appreciated.

If the file you're trying to close is an Excel workbook, and you're trying to close it using VBA in another (separate) Excel workbook, you could try the code below.

If this isn't what you're trying to do, please refer to the post above and give some more info.

Code:
Sub CloseWorkbook()
  Dim Wb As Workbook
  For Each Wb In Application.Workbooks
    If Wb.Name Like "Price List*" Then
      Wb.Close SaveChanges:=True ' or false to discard changes
      Exit Sub
    End If
  Next Wb
End Sub
 

Forum statistics

Threads
1,086,258
Messages
5,388,723
Members
402,137
Latest member
pkulkarni

Some videos you may like

This Week's Hot Topics

Top