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

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Is VBA opening the file?
If so, please post your code that opens it.
 
Upvote 0
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
 
Upvote 0
It only actually saves but the filename depends on a cell value, ParamRay's code did the trick - thanks anyway.
Is VBA opening the file?
If so, please post your code that opens it.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,296
Members
448,954
Latest member
EmmeEnne1979

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