+ Reply to Thread
Results 1 to 10 of 10

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

Hybrid View

  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,645

    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.
        On Error Resume Next
        Set wsResults = Sheets("Search-Results")
        On Error GoTo 0
        
        If wsResults Is Nothing Then
            Set wsResults = Sheets.Add
            wsResults.Name = "Search-Results"
        End If
    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

    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

  4. #4
    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.

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

    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?

  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

    Dim cell As Range
    Dim MyRange As Range
                Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(1, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
    Worksheets("Search-Results").Activate
    Range("$A$1").Select

    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



    Dim cell As Range
    Dim MyRange As Range
    Dim CS As Integer, BLNFound As Boolean
        BLNFound = False
        With ThisWorkbook
            If BLNFound = False Then
    Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Search-Results"
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
            End If
        End With

  7. #7
    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
    Dim cell As Range
    Dim MyRange As Range
                Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(1, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
    Worksheets("Search-Results").Activate
    Range("$A$1").Select

    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



    Dim cell As Range
    Dim MyRange As Range
    Dim CS As Integer, BLNFound As Boolean
        BLNFound = False
        With ThisWorkbook
            If BLNFound = False Then
    Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Search-Results"
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
            End If
        End With

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

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

    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?

  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


  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



    Private Sub Find_Click()
    For SC = 1 To Worksheets.Count
        If Worksheets(SC).Name = "Search-Results" Then
            exists = True
    Dim cell As Range
    Dim MyRange As Range
                Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(1, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
    Worksheets("Search-Results").Activate
    Range("$A$1").Select
        End If
    Next SC
    If Not exists Then
    Dim CS As Integer, BLNFound As Boolean
        BLNFound = False
        With ThisWorkbook
            If BLNFound = False Then
    Worksheets("Database1").Activate
         ActiveSheet.Range("$I$1").Value = Searchinput.Value
    With Intersect(ActiveSheet.UsedRange, Columns("A:H"))
            .Offset(, 9).ClearContents
        End With
    Set MyRange = Sheets("Database1").Range("A2:H2" & Cells(Rows.Count, 1).End(xlUp).Row)
    For Each cell In MyRange
    If InStr(cell, Range("I1")) > 0 Then
        i = cell.Row
        Range(Cells(i, 1), Cells(i, 8)).Copy
        Range("J" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
        Application.CutCopyMode = False
        End If
    Next
    Application.CutCopyMode = False
    Sheets("Database1").Range("$I$1").ClearContents
    Range("$A$1").Select
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Search-Results"
    Sheets("Database1").Range("$J:$Q").Copy Destination:=Sheets("Search-Results").Range("$A$1")
    If Sheets("Search-Results").Range("$A$2").Value = 0 Then
        Application.DisplayAlerts = False
        Sheets("Search-Results").Delete
        Application.DisplayAlerts = True
        If MsgBox("No Matches Found!", vbOK, "ATTENTION!") = vbOK Then Exit Sub
    End If
            End If
        End With
    End If
    End Sub

+ 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 hutch@edge.net 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