Solved

Excel VBA - Find value within range

Posted on 2016-09-13
7
27 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
[X]
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
  • 4
  • 2
7 Comments
 
LVL 33

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 33

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Set Variable1 = cell.Value
0
 
LVL 33

Expert Comment

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

Will check when back online tomorrow
0
 
LVL 33

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 14

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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;…
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…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

749 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