Solved

Excel VBA - Find value within range

Posted on 2016-09-13
7
25 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 32

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 32

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

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

Set Variable1 = cell.Value
0
 
LVL 32

Expert Comment

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

Will check when back online tomorrow
0
 
LVL 32

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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