+ Reply to Thread
Results 1 to 7 of 7

Check if wb exists

Hybrid View

Vinidis Check if wb exists 01-25-2022, 04:58 AM
CheeseSandwich Re: Check if wb exists 01-25-2022, 05:39 AM
Vinidis Re: Check if wb exists 01-26-2022, 08:45 AM
CheeseSandwich Re: Check if wb exists 01-26-2022, 09:26 AM
Vinidis Re: Check if wb exists 01-26-2022, 10:41 AM
CheeseSandwich Re: Check if wb exists 01-26-2022, 09:35 AM
Marc L Hi, try this … 01-26-2022, 10:23 AM
  1. #1
    Registered User
    Join Date
    01-25-2022
    Location
    Veszprem
    MS-Off Ver
    2013
    Posts
    3

    Check if wb exists

    Hi all!

    I have a little issue here. I'd like to insert a check if method with a warning message into my code, but can't get the desired result.
    I'd like to check if the workbook exits, before it's opened and copied the sheet. If not I need a message and stop the whole process, else go on.
    I've tried the 'if dir method = 0 or "" then --- else' but nothing. Determined the file and path but did nothing, only just placed a cmdbutton to the current sheet without the warning message.
    My code:
    Sub Recall()
    
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim filename As String
    
    On Error GoTo errorhandler
    
        Application.DisplayAlerts = False
    
        Set wb = Workbooks("Moulding check.xlsm")
        Set ws = ActiveSheet
    
        filename = ws.Range("C2") & " " & ws.Range("N2")
        
            Worksheets("Ures lap").Range("C2").ClearContents
            Worksheets("Ures lap").Range("N2").ClearContents
    
                Set closedBook = Workbooks.Open("H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\ " & filename & ".xlsx")
                closedBook.Sheets(filename).Copy Before:=ThisWorkbook.Sheets("Meretek")
                closedBook.Close SaveChanges:=False
    
            Kill ("H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\ " & filename & ".xlsx")
    
        wb.Activate
    
        ActiveSheet.Buttons.Add(650, 18, 110, 20).Select
            Selection.Name = "Gomb 1"
            Selection.OnAction = "SaveSheet"
        ActiveSheet.Shapes("Gomb 1").Select
            Selection.Characters.Text = "Munkalap mentese"
    
    Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
        
    errorhandler:
    Resume Next
        
    Application.DisplayAlerts = True
        
    End Sub
    This works fine, but I'd like to add a check method with a warning message.
    Can someone help?
    Thank you.
    Last edited by Vinidis; 01-25-2022 at 05:05 AM.

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,451

    Re: Check if wb exists

    The Dir method will return a file name if the file exists and a blank if not, we can use this to create a small function to get a boolean response. Something like the below:

    Sub test()
        Dim ans As Boolean, FileName As String
        
        FileName = "C:\Users\jbloggs\Desktop\test123.xlsx"
        
        ans = CheckFile(FileName)
        
        If ans = False Then
            MsgBox "File name: " & FileName & " does not exist."
            Exit Sub
        Else
            MsgBox "File name: " & FileName & " exists."
        End If
    End Sub
    
    
    
    Function CheckFile(FilePath As String) As Boolean
        ans = Dir(FilePath)
        If ans <> vbNullString Then
            CheckFile = True
        Else
            CheckFile = False
        End If
    End Function

  3. #3
    Registered User
    Join Date
    01-25-2022
    Location
    Veszprem
    MS-Off Ver
    2013
    Posts
    3

    Re: Check if wb exists

    Hi CheeseSandwich!

    I think I'm doing something wrong. If I modify my code with your solution the result is always the same message: "File name: xxxx does not exist."
    Where should I insert your code to get the proper result?
    Can you please explain?
    Thank you.

  4. #4
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,451

    Re: Check if wb exists

    Is there a space before your file name as the below code will create a file location like:
    "H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\ FileName.xlsx"
    notice the space before the FileName part - is this intentional?

    Should it not be:
    "H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\FileName.xlsx"

    Workbooks.Open("H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\ " & Filename & ".xlsx")
    This could be why we are drawing a blank as it can't find the file with the above file path.

  5. #5
    Registered User
    Join Date
    01-25-2022
    Location
    Veszprem
    MS-Off Ver
    2013
    Posts
    3

    Re: Check if wb exists

    Yes, it was a mistake, but in this case did not matter because I did the same typo when the sheet was saved. So the file name had that space too.

    After trying your combined code it works.
    I modified the first filename like you did (and refer that after the .open method too), maybe that was the problem.

    Your solution works fine, thank you.

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,451

    Re: Check if wb exists

    If you wanted to incorporate the check then it would look something like the below (untested)

    Sub Recall()
    
        Dim ws As Worksheet
        Dim wb As Workbook
        Dim FileName As String
        Dim ans As Boolean, flName As String
        
        Set wb = ThisWorkbook
        Set ws = ActiveSheet
        
        FileName = ws.Range("C2") & " " & ws.Range("N2")
        
        Worksheets("Ures lap").Range("C2").ClearContents
        Worksheets("Ures lap").Range("N2").ClearContents
        
        flName = "H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\" & FileName & ".xlsx"
        ans = CheckFile(flName)
        If ans = False Then
            MsgBox "File name: " & flName & " does not exist."
            Exit Sub
        End If
        
        Set closedBook = Workbooks.Open(flName)
        closedBook.Sheets(FileName).Copy Before:=ThisWorkbook.Sheets("Meretek")
        closedBook.Close SaveChanges:=False
        
        Kill (flName)
        
        wb.Activate
        
        ActiveSheet.Buttons.Add(650, 18, 110, 20).Select
        Selection.Name = "Gomb 1"
        Selection.OnAction = "SaveSheet"
        ActiveSheet.Shapes("Gomb 1").Select
        Selection.Characters.Text = "Munkalap mentese"
        
        Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
        
    End Sub
    
    Function CheckFile(FilePath As String) As Boolean
        ans = Dir(FilePath)
        If ans <> vbNullString Then
            CheckFile = True
        Else
            CheckFile = False
        End If
    End Function

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this …


    A VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
      Const 
    ".xlsx""H:\CD\01 Gyartaskozi ellenorzes\Lezart gyartasok\"
        Dim F$
            F = Workbooks("
    Moulding check.xlsm").ActiveSheet.[C2&" "&N2]
            If Dir(P & F & E) = "" Then MsgBox "
    File not found", 48: Exit Sub
        With Workbooks.Open(P & F & E, 0)
            .Sheets(F).Copy ThisWorkbook.Sheets("
    Meretek")
            .Close False
        End With
            Kill P & F & E
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to check if sheet name exits
    By robrobet in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2015, 01:00 PM
  2. [SOLVED] if sheet2 not exits then creat one
    By asdzxc in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-24-2013, 02:27 AM
  3. if sheet2 not exits then creat one
    By asdzxc in forum Excel General
    Replies: 1
    Last Post: 09-21-2013, 11:47 PM
  4. Reprotect after macro exits.
    By D1TrueGod in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2008, 12:34 AM
  5. Excel exits without prompts when tabbing. Please HELP!
    By pavlouc in forum Excel General
    Replies: 0
    Last Post: 07-31-2007, 11:27 AM
  6. [SOLVED] Sub Exits Unexpectedly
    By Walker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-25-2005, 06:06 PM
  7. way to run sub after user exits textbox
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2005, 09:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1