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

i200yrs

New Member
Joined
Dec 18, 2019
Messages
22
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

i200yrs

New Member
Joined
Dec 18, 2019
Messages
22
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
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 ")
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
thanks for your feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,127,838
Messages
5,627,181
Members
416,227
Latest member
Abid Fattani

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
Top