Solved

Excel VBA - Find value within range

Posted on 2016-09-13
7
39 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

624 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