Results 1 to 5 of 5

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

  1. #1
    Board Regular
    Join Date
    Nov 2012
    Posts
    231
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  2. #2
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    51,032
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

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

    Is VBA opening the file?
    If so, please post your code that opens it.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    Board Regular ParamRay's Avatar
    Join Date
    Aug 2014
    Location
    England, UK
    Posts
    1,195
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    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
    Windows 10, Excel 365

  4. #4
    Board Regular
    Join Date
    Nov 2012
    Posts
    231
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    It only actually saves but the filename depends on a cell value, ParamRay's code did the trick - thanks anyway.
    Quote Originally Posted by Joe4 View Post
    Is VBA opening the file?
    If so, please post your code that opens it.

  5. #5
    Board Regular
    Join Date
    Nov 2012
    Posts
    231
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Excellent this worked a treat thank you for your help, it's much appreciated.

    Quote Originally Posted by ParamRay View Post
    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

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •