Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA - Find value within range

Posted on 2016-09-13
7
Medium Priority
?
51 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

719 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