VBA to look up criteria in one column and return values from another column

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm looking for a VBA solution that will:

1. Look at the value in cell G7 in the sheet named "Criteria" (it will be a string of simple text, eg. Black Cat).
2. Then go to the Range E4:E1000 in the sheet named "Info" and find every instance that matches the value in G7 on the "Criteria" sheet.
3. Then return all the values in the Range F4:F1000 in the sheet named "Info" that are adjacent to the matching values found in the Range E4:E1000.
4. The values need to be returned into cell C6 downwards in sheet "Criteria" as one block, ie. no empty rows.

Hopefully this makes sense.
Any help much appreciated.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Untested - try on a copy of your workbook.
VBA Code:
Sub cjcass()
Dim wsht1 As Worksheet, Wsht2 As Worksheet
Dim LookFor As String, Rinfo As Range, Rfnd As Range, Output() As Variant, Ct As Long
Set wsht1 = Sheets("Criteria"): Set Wsht2 = Sheets("Info")
LookFor = wsht1.Range("G7").Value: Set Rinfo = Wsht2.Range("E4:E1000")
With Rinfo
    Set Rfnd = .Find(what:=LookFor, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
    If Rfnd Is Nothing Then
        MsgBox "The search criteria " & LookFor & " is not found - exiting sub"
        Exit Sub
    Else
        adr = Rfnd.Address
    End If
    Do
        Ct = Ct + 1
        ReDim Preserve Output(1 To Ct)
        Output(Ct) = Rfnd.Offset(0, 1).Value
        Set Rfnd = .FindNext(Rfnd)
        If Rfnd Is Nothing Then Exit Do
        If Rfnd.Address = adr Then Exit Do
    Loop
End With
wsht1.Range("C6").Resize(Ct, 1).Value = Application.Transpose(Output)
End Sub
 
Upvote 0
Solution
Untested - try on a copy of your workbook.
VBA Code:
Sub cjcass()
Dim wsht1 As Worksheet, Wsht2 As Worksheet
Dim LookFor As String, Rinfo As Range, Rfnd As Range, Output() As Variant, Ct As Long
Set wsht1 = Sheets("Criteria"): Set Wsht2 = Sheets("Info")
LookFor = wsht1.Range("G7").Value: Set Rinfo = Wsht2.Range("E4:E1000")
With Rinfo
    Set Rfnd = .Find(what:=LookFor, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
    If Rfnd Is Nothing Then
        MsgBox "The search criteria " & LookFor & " is not found - exiting sub"
        Exit Sub
    Else
        adr = Rfnd.Address
    End If
    Do
        Ct = Ct + 1
        ReDim Preserve Output(1 To Ct)
        Output(Ct) = Rfnd.Offset(0, 1).Value
        Set Rfnd = .FindNext(Rfnd)
        If Rfnd Is Nothing Then Exit Do
        If Rfnd.Address = adr Then Exit Do
    Loop
End With
wsht1.Range("C6").Resize(Ct, 1).Value = Application.Transpose(Output)
End Sub
Works beautifully, thank you very much indeed :)
 
Upvote 0
When marking a reply as the solution, please mark the post that originally contained the solution, not your own post acknowledging that some other post is the solution.
I have gone ahead and updated this for you.
 
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,512
Members
449,167
Latest member
jrob72684

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top