none
vba to close workbook based on first part of its name RRS feed

  • Question

  • Hi All

    I've got a code which works well:

    Workbooks("FTV - CENEO Quickview (Panasonic) - tool v78 robo.xlsm").Close SaveChanges:=False

    but I need to adjust it to close all workbooks with the first part of the name:

    "FTV - CENEO Quickview (Panasonic) - tool"

    second part of the name:

    " v78 robo.xlsm"

    could change and in those cases the code presented as first doesn't work

    I tried these ones but it is not working:

    Workbooks("FTV - CENEO Quickview (Panasonic) - tool" & ".*").Close SaveChanges:=False

    Workbooks("FTV - CENEO Quickview (Panasonic) - tool*" & ".xlsm").Close SaveChanges:=False

    Hot to fix it?

    Thursday, February 19, 2015 2:47 PM

Answers

  • Try this:

    Sub CloseWorkbooks()
        Dim i As Long
        For i = Workbooks.Count To 1 Step -1
            If Workbooks(i).Name Like "FTV - CENEO Quickview (Panasonic) - tool*" Then
                Workbooks(i).Close SaveChanges:=False
            End If
        Next i
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, February 19, 2015 3:26 PM

All replies

  • Try this:

    Sub CloseWorkbooks()
        Dim i As Long
        For i = Workbooks.Count To 1 Step -1
            If Workbooks(i).Name Like "FTV - CENEO Quickview (Panasonic) - tool*" Then
                Workbooks(i).Close SaveChanges:=False
            End If
        Next i
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, February 19, 2015 3:26 PM
  • Thanks Hans

    Working great

    Friday, February 20, 2015 9:57 AM