+ Reply to Thread
Results 1 to 10 of 10

VBA Check if Sheet Exists, if it does, run code. Test File Attached

  1. #1
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Search Results Test.xlsm

    Hello, I'm a VBA Novice, (Trying to learn) and have the following issue, I'm trying to create a Search Function which will search data on my ("Database1") sheet and copy it to a new sheet called ("Search-Results"). Currently my code checks to see if ("Search-Results") exists, if it does not, it then creates it and pastes the data to it. This part of my code is working exactly as I want. As soon as ("Search-Results") is created it switches to it and shows the user the results of the search they just ran. My problems start when ("Search-Results") already exists. So when ("Search-Results") already exists, the code should then simply skip the sheet creation line of code and do everything else the same, but I am having trouble with achieving this. Atm, i have an IF MsgBox line that states "One or More Matches Found!" if the user's search has matches. I would like this MsgBox to not be there. The reason I dont simply delete it is that when I do it breaks the code that checks if the sheet already exists and it trys to create a 2nd sheet called ("Search-Results") which ends in failure. So to best demonstrate whet I would like to achieve, plz download the attached copy and click the "Enter Data" button which brings up my Search Userform, Input "Allen" as a Search and you will see the code creates ("Search-Results") and shows the results found for "Allen". (This is the part that works) Then input "John" and hit Search again and you will see the difference. All help is greatly appreciated!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    You could use this right at the start to check for the 'Search-Results' sheet.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Thanks for your reply!
    When I try to run that code as stand alone it gives me a debug error.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Where do you get the error?

    Do you get the error when the 'Search-Results' sheet exists?

    What level do you have error trapping set at?

  5. #5
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    If wsResults Is Nothing Then

    Above is the line that gave me an error, and it was when the sheet did not exist. Not sure what error trapping is sorry.. im very new to vba

  6. #6
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Please Login or Register  to view this content.

    The code above works great stand alone and obviously only works if the Sheet "Search-Results" already exists

    The code below works great stand alone and obviously only works if the Sheet "Search-Results" does NOT exist.

    I need to bring these 2 codes together and make them work as one



    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    How exactly did you try what I suggested and what was the error message?

  8. #8
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached


  9. #9
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Quote Originally Posted by unit285 View Post
    Please Login or Register  to view this content.

    The code above works great stand alone and obviously only works if the Sheet "Search-Results" already exists

    The code below works great stand alone and obviously only works if the Sheet "Search-Results" does NOT exist.

    I need to bring these 2 codes together and make them work as one



    Please Login or Register  to view this content.

    Is there anyone who can help me put these 2 codes together and make them work as 1 macro?

  10. #10
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Check if Sheet Exists, if it does, run code. Test File Attached

    Was able to stitch it together myself. Thanks for your efforts Norie



    Please Login or Register  to view this content.

+ 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] VBA - How can I test if a certain sheet already exists?
    By StuCram in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-15-2014, 11:50 AM
  2. Need code to check if file exists then activate conditional macro
    By mattheritage in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-04-2014, 12:00 PM
  3. VBA code to check #REF error exists in Excel file
    By KPK in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-02-2013, 01:56 AM
  4. How to programmatically test whether VBA code exists in an Excel file
    By Paul Martin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-12-2013, 09:14 AM
  5. [SOLVED] Test if file exists even if hidden
    By djblois1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-10-2012, 11:43 AM
  6. Test if sheet exists
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-12-2009, 12:12 PM
  7. How can I test whether or not an external file exists?
    By WilliamCRodgers in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-15-2009, 06:59 PM

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