Search a value in a column-B and return the values from column-A

i200yrs

New Member
Joined
Dec 18, 2019
Messages
43
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
Hello Excel Experts...need some help...see table below:
A B
1 Apple
2 Ball
3 Apple
4 Cat
5 Dog
I want an excel macro like if the user key-in "Apple" and click search will return values from column-A and list it in Column-C.
Like below result:
C
1
3

Hoping for usual supports...thanks
 
code below assumes that Sheet2 cell D4 contains a value when the code is run
VBA Code:
Sub FindText()
    Dim LastCel As Range, Found As Range, Addr As String
    Set LastCel = Sheets("Sheet2").Cells(Rows.Count, "D")
    Range(LastCel.Offset(5 - LastCel.Row), LastCel).ClearContents
    Set Found = Sheets("Sheet3").Range("B2:Z500").Find(Sheets("Sheet1").Range("A1").text, LookIn:=xlValues)
    If Not Found Is Nothing Then
        Addr = Found.Address
        Do
            LastCel.End(xlUp).Offset(1) = Found.Offset(, -Found.Column + 1)
            Set Found = Sheets("Sheet3").Range("B2:Z500").FindNext(Found)
        Loop While Not Found.Address = Addr
    End If
End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
code below assumes that Sheet2 cell D4 contains a value when the code is run
VBA Code:
Sub FindText()
    Dim LastCel As Range, Found As Range, Addr As String
    Set LastCel = Sheets("Sheet2").Cells(Rows.Count, "D")
    Range(LastCel.Offset(5 - LastCel.Row), LastCel).ClearContents
    Set Found = Sheets("Sheet3").Range("B2:Z500").Find(Sheets("Sheet1").Range("A1").text, LookIn:=xlValues)
    If Not Found Is Nothing Then
        Addr = Found.Address
        Do
            LastCel.End(xlUp).Offset(1) = Found.Offset(, -Found.Column + 1)
            Set Found = Sheets("Sheet3").Range("B2:Z500").FindNext(Found)
        Loop While Not Found.Address = Addr
    End If
End Sub
THIS ONE IS PERFECT! THANKS A LOT TO YOU YONGLE ")
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

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