Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


In Excel VBA, Can I do a find on a range for all cells containing a value and have a range with just those cells found returned?

Posted on 2013-05-29
Medium Priority
Last Modified: 2013-06-06
In Excel VBA, Can I do a find on a range for all cells containing a value and have a range with just those cells found returned?

This would more than likely be a non-contiguous range.  Is this possible?  If so, how?

Let's say I wanted to find all cells in a range with the text "Joe" in them and then get a range of ONLY those cells that had that text in them.  What would the code look like for this?
Question by:cashonly
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 35

Accepted Solution

[ fanpages ] earned 2000 total points
ID: 39205917

Here is one method...

Option Explicit
Public Sub Q_28142220()

' --------------------------------------------------------------------------------------------------------------
' [ http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_28142220.html ]
' Question Channel: Experts Exchange > Software > Office / Productivity > Office Suites > MS Office
' ID:               28142220
' Question Title:   In Excel VBA, Can I do a find on a range for all cells containing a value and have a range
'                   with just those cells found returned?
' Question Asker:   cashonly                                  [ http://www.experts-exchange.com/M_1081164.html ]
' Question Dated:   2013-05-29 at 20:51:53
' Expert Comment:   fanpages                                   [ http://www.experts-exchange.com/M_258171.html ]
' Copyright:        (c) 2013 Clearlogic Concepts (UK) Limited                           [ http://NigelLee.info ]
' --------------------------------------------------------------------------------------------------------------

  Dim objCell                                           As Range
  Dim objStart                                          As Range
  Dim objRange                                          As Range
  Set objCell = ActiveSheet.UsedRange.Find("Joe")
  Set objStart = objCell
  While Not (objCell Is Nothing)
      If (objRange Is Nothing) Then
         Set objRange = objCell
         Set objRange = Union(objRange, objCell)
      End If ' If (objRange Is Nothing) Then
      Set objCell = ActiveSheet.UsedRange.Find(What:="Joe", _
      If objCell.Address = objStart.Address Then
         Set objCell = Nothing
      End If ' If objCell.Address = objStart.Address Then
  If Not (objRange Is Nothing) Then
     Set objRange = Nothing
  End If ' If Not (objRange Is Nothing) Then
  Set objStart = Nothing
  Set objCell = Nothing

End Sub

Open in new window

For convenience, please see the attached workbook.



Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question