Solved

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

Posted on 2014-04-17
3
351 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 80

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 80

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now