Solved

Excel VBA - Find value within range

Posted on 2016-09-13
7
22 Views
Last Modified: 2016-10-20
I have the need in Excel vba to find a value within a range that will change.  I want to start my find in cell B3 and continue to look down column B for a specific word.  When it finds that word i want to set a variable in my code for that cell.  I want it to continue to look down from B3 until it hits a blank cell.  When it hits the blank cell i also want to record a variable for that blank cell.
0
Comment
Question by:jnikodym
  • 4
  • 2
7 Comments
 
LVL 31

Expert Comment

by:Rob Henson
ID: 41796269
Try this:

Sub FindWord()
    
    FindValue = Range("C5")
    
    Range("B3:B43").Select
    Selection.Find(What:=FindValue, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Set Variable1 = Cell.Value
    ActiveCell.Offset(1, 0).Range("A1").Select
    If ActiveCell.Value = "" Then
    SetVariable2 = Cell.Value
    Else
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Set Variable2 = Cell.Value
    End If
End Sub

Open in new window


This will select Range B3 to B43
Search for variable value from range C5
When found it will the value of the Active cell and Set Variable1 to the cell value
It then moves down one cell and checks if that is the next blank,
   if it is it sets the variable2
   if not it does the End Down which will take the cursor to the last populated cell below and then one more to set variable2

Thanks
Rob H
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 41796284
Amended code to allow for cell being at bottom of list:

Sub FindWord()
    
    FindValue = Range("C5")
    
    Range("B3:B43").Select
    Selection.Find(What:=FindValue, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    SetVariable = Cell.Value
    ActiveCell.Offset(1, 0).Range("A1").Select
    
    If ActiveCell.Value = "" Then
        SetVariable2 = Cell.Value
    Else
        ActiveCell.Offset(1, 0).Range("A1").Select
    If ActiveCell.Value = "" Then
        SetVariable2 = Cell.Value
    Else
        ActiveCell.Offset(-1, 0).Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Range("A1").Select
        SetVariable2 = Cell.Value
    End If
    End If
End Sub

Open in new window

0
 

Author Comment

by:jnikodym
ID: 41796286
i get an object required error on the

Set Variable1 = cell.Value

Also, you have range B3:B43 as the search range.  I want to start at cell B3 and keep going until it hits a blank cell
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jnikodym
ID: 41796299
i get an object required error on the

Set Variable1 = cell.Value
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 41796698
Rushed out to catch train without testing.

Will check when back online tomorrow
0
 
LVL 31

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 41797592
OK, try this:

Sub FindWord()
    
    Dim Variable1 As String
    Dim Variable2 As Long
    
    FindValue = Range("C5")
    
    Range("B3").Select
    Selection.End(xlDown).Select
    LR = ActiveCell.Row
    Variable2 = LR + 1
    Range("B3:B" & LR).Select
    
    Selection.Find(What:=FindValue, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Variable1 = Cell.Value
    
End Sub

Open in new window


This will start at B3 and do End Down to find last row with data, next row is therefore first blank.
It then uses From B3 to that row to search for the value in C5 and takes the value of the cell where it finds it.

Hope that now does what you want.

Thanks
Rob
0
 
LVL 13

Expert Comment

by:frankhelk
ID: 41851693
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Rob Henson (https:#a41797592)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now