Solved

VBA that will find a value or variable in a multi-row block of cells

Posted on 2014-04-17
3
357 Views
Last Modified: 2014-04-18
I need to find value x in this range [M62:AG66]. Is there a built in function like that?

Something like:   x = Range("M62:AG62").find (What:= [A4])
But Multi-Row:    x = Range("M62:AG66").find (What:= [A4])

If not, is there an even more compact way than this code to get it to search down, then across?
Dim cel As Range, FindRng As Range, x As Long
For Each cel In [M62:AG62]
cel.Select
    Set FindRng = Range(cel, cel.Offset(4)) 
    For Each cel2 In FindRng
    If cel2 = [A4] Then
        x = cel.Column - 11
        GoTo here
    End If
    Next cel2
Next cel
here:

Open in new window


Thanks,
John
0
Comment
Question by:gabrielPennyback
  • 2
3 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 40008174
John,
You could use the Find method as shown in the snippet below.

Brad
Sub FindIt2()
Dim cel As Range, FindRng As Range, FindVal As Range, x As Long
Set FindRng = Range("M62:AG62").Resize(5)
Set FindVal = Range("A4")
Set cel = FindRng.Find(FindVal, LookAt:=xlWhole, SearchOrder:=xlByColumns)
If Not cel Is Nothing Then
    x = cel.Column - 11
    cel.Select
    'here would be here
End If
End Sub

Open in new window

0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 40008181
One fine point with the Find method is that the search starts with the cell after the top left cell in the range. So I should correct the code to this:
Sub FindIt2()
Dim cel As Range, FindRng As Range, FindVal As Range, x As Long
Set FindRng = Range("M62:AG62").Resize(5)
Set FindVal = Range("A4")
Set cel = IIf(FindVal.Value = FindRng.Cells(1, 1).Value, FindRng.Cells(1, 1), FindRng.Find(FindVal, LookAt:=xlWhole, SearchOrder:=xlByColumns))
If Not cel Is Nothing Then
    x = cel.Column - 11
    cel.Select
    'here would be here
End If
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 40009116
... and his eyes were opened.  Thanks, Brad! Great to hear from you.   ~ John
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now