Library code snippets

Excel worksheet search/find function

Having problems with finding the right worksheet? This code makes it easier find what you are looking for. The search criteria is the worksheet names that is put into a combobox automatically. when clicking on the name or writing it, the worksheet with that name appears.Add a userform, userform1. Put a combobox on it. Write this in your module (Module1):

'makes the "searchform" appear when you start
Sub findWorkSheet() 'call this function!!
    UserForm1.Show
End Sub

'To make things go our way, we need to write some code. Here is 4 functions. See the comments.
Sub populateCombo() 'populates the combobox
Dim tempWorksheet As Worksheet
Dim x, totalSheets
x = 1
ComboBox1.Clear() 'Clear combo box
totalSheets = ThisWorkbook.Sheets.Count 'counts the sheets in the workbook
Do While x <= totalSheets
  Set tempWorksheet = Sheets(x) 'The tempWorksheets has now the content of the x worksheet
  ComboBox1.AddItem (test.Name)'Put the worksheets name in the combobox
  x = x + 1 'next worksheet!
Loop

End Sub

Sub DoesSheetExist()' Get the worksheet you are looking for to appear
Dim wSheet As Worksheet

On Error Resume Next
   Set wSheet = Sheets(ComboBox1.Text)
       If wSheet Is Nothing Then 'Does not exist
           Set wSheet = Nothing
           On Error GoTo 0
       Else 'Exist
           wSheet.Activate ' open the right worksheet
           Set wSheet = Nothing
           On Error GoTo 0
       End If
End Sub

Private Sub ComboBox1_Change()
DoesSheetExist 'call function
End Sub

Private Sub UserForm_Activate()
populateCombo' when starting the combobox is filled
ComboBox1.SetFocus
End Sub

Comments

  1. 07 Jul 2009 at 09:12

    hi everyone,

    i put the code as it is in my vba, and created the 2 sheets (userform, userform1) but i Always end up having a problem with these 2 parts,

    Set tempWorksheet = Sheets(x) 'The tempWorksheets has now the content of the x worksheet (says syntax error)

    Set wSheet = Sheets(ComboBox1.Text) (says Sub or function not defined and marks Sheets in the line)

    can anyone please help me ? would be really appreciated , thanks

  2. 07 Jul 2009 at 09:11

    hi everyone,

    i put the code as it is in my vba, and created the 2 sheets (userform, userform1) but i Always end up having a problem with these 2 parts,

    Set tempWorksheet = Sheets(x) 'The tempWorksheets has now the content of the x worksheet (says syntax error)Set wSheet = Sheets(ComboBox1.Text) (says Sub or function not defined and marks Sheets in the line)

    can anyone please help me ? would be really appreciated , thanks

  3. 21 Jun 2007 at 19:00
    In the populateCombo sub, I had to change the line

    ComboBox1.Clear()

    to

    ComboBox1.Clear

    No arguments required...

    VERY useful by the way - I'd been trying to think of a way to do this efficiently!

    - Morgan













  4. 16 Jan 2006 at 15:22

     Set tempWorksheet = Sheets(x) 'The tempWorksheets has now the content of the x worksheet
     ComboBox1.AddItem (tempWorksheet.Name) 'Put the worksheets name in the combobox


    change test.Name to tempWorksheet.Name

  5. 26 Apr 2005 at 00:02

    Would anyone be so kind as to tell me how to select multiple worksheets from a workbook? Specifically, the workbook has about 100 worksheets, and I'd like to pull out around 50 specific ones. Can anyone help??? Thank you very much.



    -yk

  6. 10 Mar 2005 at 15:08

    using System.Runtime.InteropServices;


    excelObj.Quit();
    Marshal.ReleaseComObject(worksheet);
    Marshal.ReleaseComObject(sheets);
    Marshal.ReleaseComObject(workbook);
    Marshal.ReleaseComObject(excelObj);
    Marshal.ReleaseComObject(excelObj);
    excelObj = null;


    and then do a Garbage Collect.  Thats what it took for me...  Hope it helps.

  7. 26 Jun 2004 at 09:39


  8. 26 Jun 2004 at 09:00

    Thanks for good topic.

  9. 11 Aug 2002 at 05:51

    Hi I Need to write a component which will close all instances of the excel files,those are opend from different sources.Can u help me on this.

  10. 28 Jun 2002 at 18:16

    Not all of the code shall be in the module. So I write this so there becomes no misunderstanding!


    [3]Write this in your module (Module1): [/3]


    'makes the "searchform" appear when you start
    Sub findWorkSheet() 'call this function!!
       UserForm1.Show
    End Sub


    The rest of the code is "on the" userform.


    Also there should be "tempworksheet.name" and NOT "test.name" in the function Sub populateCombo() !!!




    Per A. Bergersen

  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Excel worksheet search/find function.

Leave a comment

Sign in or Join us (it's free).

Per A. Bergersen

We'd love to hear what you think! Submit ideas or give us feedback