Consulting

Results 1 to 8 of 8

Thread: If file is open or if file exist

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    If file is open or if file exist

    Hello I have the following code (create an workbook):

    Private Sub CommandButton116_Click()
        Dim wb As Workbook
        Set wb = Workbooks.Add
        Application.DisplayAlerts = False
        wb.SaveAs FileName:=ThisWorkbook.Path & "\QUO-.xls"
    Dim LastRow As Long
          Set wb = Workbooks.Open(ThisWorkbook.Path & "\QUO-.xls")
        With wb
        'copy range
        End With
        wb.save    
    End Sub
    I like to add the following: if the workbook exist to add an suffix?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Private Sub CommandButton116_Click()
        Dim wb As Workbook
        Dim filename As String
        Dim LastRow As Long
    filename = ThisWorkbook.Path & "\QUO-.xls"
        On Error Resume Next
            Set wb = Workbooks(filename)
        On Error GoTo 0
        If wb Is Nothing Then
    Set wb = Workbooks.Add
            Application.DisplayAlerts = False
            wb.SaveAs filename
        End If
    With wb
             'copy range
        End With
        wb.Save
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You can use DIR() to determine if the file exits.

    To create a unique sequential name, see: http://www.vbaexpress.com/kb/getarticle.php?kb_id=1041

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Hello Bob

    I mean an suffix for file name like: QUO-1, QUO-2 etc

  5. #5
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Kenneth, thx for suggestion but that level of vba is to high for me

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by white_flag
    Kenneth, thx for suggestion but that level of vba is to high for me
    No, it's easy

    filename = Dir(ThisWorkbook.Path & "\QUO-.xls", vbNormal)
        If filename = "" Then
    MsgBox "not found"
        Else
    MsgBox "found"
        End If
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by white_flag
    Hello Bob

    I mean an suffix for file name like: QUO-1, QUO-2 etc
    Not sure what you mean.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=1008

    this is what I need, Bob thx for suggestions.. here, it is nice and cold

Posting Permissions

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