Click here to Skip to main content
15,886,026 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Excel Search Tool (Macro)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
12 Jun 2017CPOL1 min read 28.9K   676   6   10
This is a simple macro script to search and retrieve individual cells in an Excel document.

Introduction

This macro script is used to search for specific cells in Excel documents and copy the cells into a separate sheet.

Background

This is my first project on CodeProject, and my first VBA script, so I am open to suggestions on how to improve my code! I wrote this script along with the help of my co-workers to search through our documents quickly and retrieve data.

Using the Code

The code is meant to be imported into an Excel document. The file is linked with the .bas extension.

Error Catch

VBScript
On Error GoTo ErrorCatch

Variables

VBScript
Dim WorkSheet_Count As Integer
Dim index As Integer
Dim columnCount As Integer
Dim inputRange As Range
Dim cellContent As String

Initial Headers to Define Columns

VB
'Add all column names.
Sheets("SearchResults").Range("A1:E1").Value = _
    Array("Customer Name", "Example Column", 
    "Example Column", "Example Column", "Example Column")

Main Body

VB
'Suggested by Dennis (<a href="https://www.codeproject.com/script/Membership/View.aspx?mid=169517">
DW1962</a>) to prevent flickering of the screen while processing data
Application.ScreenUpdating = False

' Set WS_Count equal to the number of worksheets in the active workbook.
WorkSheet_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.
         For index = 2 To WorkSheet_Count
           
            'Search for the string "Customer Name"
            Set inputRange = ActiveWorkbook.Worksheets(index).Cells.Find("Customer Name:")
            
               'If it is found, continue
               If Not inputRange Is Nothing Then
                   cellContent = ActiveSheet.Cells(inputRange.Row, _
                   (inputRange.Column + 1)).Address(False, False)
                   Sheets(ActiveWorkbook.Worksheets(index).Name).Range(cellContent).Copy Worksheets_
                   ("SearchResults").Range("A" & index)
            
               'If it is empty, set the cell value to "No Information"
               Else
                   Sheets("SearchResults").Range_
                   ("A" & index).Value = "No Information"
            
               End If
               Set inputRange = Nothing

         'Move to the next sheet to search       
          Next index


    'Suggested by Dennis (<a href="https://www.codeproject.com/script/Membership/View.aspx?mid=169517">
                           DW1962</a>) to "fix the columns' width"           
    '------Loop through and Auto Fit all columns-----'
    For columnCount = 1 To ActiveSheet.UsedRange.Columns.Count
 
    Columns(columnCount).EntireColumn.AutoFit
 
    Next columnCount

    Exit Sub

'Here is the catch for errors
ErrorCatch:
    MsgBox "There was an error while searching for the cell"

End Sub

To find the cell, you need to replace the string in inputRange to whatever you want to search for:

VBScript
Set inputRange = ActiveWorkbook.Worksheets(index).Cells.Find_
("Whatever you want to search for here")

Also, you need to choose whatever you want your sheet that collects the search information to be:

VBScript
Sheets(ActiveWorkbook.Worksheets(index).Name).Range(cellContent).Copy Worksheets_
("Whatever sheet name you want").Range("A" & index)

Points of Interest

I learned much about how to program macros from making this project, and I hope it will help some of you as well!

Also, I got the worksheet looper from this Microsoft article.

How to Import the Macro

  1. Open Excel and then click Developer > Visual Basic.
  2. To import the macro, click File > Import File, go to the folder where you exported your macro, select the file, and then click Open.
  3. Save the module.
  4. Create a new sheet named "SearchResults" (whatever sheet name you specified in this line):

    Sheets(ActiveWorkbook.Worksheets(index).Name).Range(cellContent).Copy Worksheets("Whatever sheet name you want").Range("A" & index)

  5. Run the macro and gather results.

History

  • V1.0 Released 12/13/2016
  • V1.1 Released 12/15/2016
    • Changed code, implementing suggestions
    • Updated script in zip file

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance Engineering
United States United States
I am currently a high school senior, preparing to study in Computer Engineering at IUPUI.
I have programmed for about 6 years, mostly in C++.
My current job is pushing me to become more adept in various programming languages, including C# and VBA.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1236439013-Jun-17 20:53
Member 1236439013-Jun-17 20:53 
QuestionSome quick improvements - Part 3 Pin
DW196214-Dec-16 8:40
DW196214-Dec-16 8:40 
GeneralRe: Some quick improvements - Part 3 Pin
LukeCodes15-Dec-16 2:06
LukeCodes15-Dec-16 2:06 
GeneralRe: Some quick improvements - Part 3 Pin
DW196215-Dec-16 3:41
DW196215-Dec-16 3:41 
GeneralRe: Some quick improvements - Part 3 Pin
LukeCodes15-Dec-16 4:51
LukeCodes15-Dec-16 4:51 
GeneralRe: Some quick improvements - Part 3 Pin
DW196216-Dec-16 3:39
DW196216-Dec-16 3:39 
QuestionSome quick improvements - Part 2 Pin
DW196214-Dec-16 8:30
DW196214-Dec-16 8:30 
QuestionSome quick improvement Pin
DW196214-Dec-16 7:22
DW196214-Dec-16 7:22 
QuestionCan't respond directly as the original thread was closed with the approval of this. Pin
OriginalGriff13-Dec-16 21:33
mveOriginalGriff13-Dec-16 21:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.