You are here:

Excel/faster search method?


Hi Tom,
I have a working search function on my spreadsheet, my question is can I make this any quicker and use a more efficient method of coding it in VBA?

I have number of worksheets with data I wish to reference in column D, the cells vary in number from 2 to 115 (d2:D115). I am currently using a select case method and have included a snippet below. This code is repeated for each worksheet.
private sub search()
'   declare variables
Dim cell As Range
   Select Case QualCategory
       Case Is = "Gas Utilisation (Downstream)"
'   Set the area within the sheet to be searched
         For Each cell In Range("d2: d115")
         If cell.Value = QualNum Then
         Category = cell.Offset(0, 2).Value
         msg = msg & Category
         MsgBox msg

         Exit Sub
         End If
         Next cell

this is repeated for every worksheet, instead of coding for when a user input is not found I have used an error handler like below

   Category = "Not Recognised by ACS"
   msg = msg & Category
   MsgBox msg

   Exit Sub

It seemed simpler to write this once and treat the not found as an error rather than duplicating the code 8 times.

Is it possible to cycle my search through the worksheets without having to specify the name of each one? and if so how could I do this? my concern really is that coding is not excessive for 8 sheets but what do I do when I have say 50 sheets or more?

kind regards



look like you are using a userform (QualSearch.hide) and I don't know if you are doing exactly the same thing to each sheet and so forth but here is some general pseudo code that show looping through  sheets and using the worksheet function countif to determine if a value exists in column D.  It processes all sheets but excludes the "Introduction" sheet.

Dim sh as worksheets, r as range
for each sh in worksheets
if lcase( <> "introduction" then
 set r = sh.Range("D:D")
 if application.countif(r, Qualnum) > 0 then
   ' action to take
end if

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.

[an error occurred while processing this directive]