Search worksheet and return all values in column directly below reference cell

mattm1981

New Member
Joined
Feb 13, 2023
Messages
9
Office Version
  1. 365
Hi, is is possible for me to write a formula for excel to search a worksheet for a specific value(s) and return all values in the column directly below where it finds the result? The use case is to extract data from property tenancy schedules. The schedules are all different formats so I would like Excel to search all columns and if it finds 'Lease expiry date' in say G16, then it will return all values from G17 down to say G2000 and paste to another worksheet.
Thanks for the help
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
VBA Code:
Sub ExtractData()

    Dim searchValue As String
    Dim searchRange As Range
    Dim resultRange As Range
    Dim foundCell As Range
    Dim lastRow As Long
    Dim outputRow As Long
   
    'Set the search value to look for
    searchValue = "Lease expiry date"
   
    'Set the search range to look for the search value
    Set searchRange = ActiveSheet.UsedRange
   
    'Find the first occurrence of the search value in the search range
    Set foundCell = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
   
    'If the search value is found
    If Not foundCell Is Nothing Then
        'Set the result range to be the column directly below where the search value is found
        lastRow = ActiveSheet.Cells(Rows.Count, foundCell.Column).End(xlUp).Row
        Set resultRange = Range(foundCell.Offset(1, 0), Cells(lastRow, foundCell.Column))
       
        'Copy the result range to a new worksheet
        outputRow = 1
        For Each cell In resultRange
            Sheets("Output").Cells(outputRow, 1) = cell.Value
            outputRow = outputRow + 1
        Next cell
    End If
   
End Sub
 
Last edited by a moderator:
Upvote 0
Thanks that worked perfectly!!
Forgive my ignorance but how would I then expand this to:

1) Add alternatives to "Lease expiry date" - for example "Lease end date"? There are probably 20 different ways this same information is headed on the dataset so I'd like to cover all the possible alternatives
2) Perform the same exercise but for a different value i.e "Tenant Name"? I'm basically looking to pull all the info on the sample tabs across to the Output.

I've tried to do this by copying in the above but have rarely used VBA so struggling to get it to work.

Thanks again
 
Upvote 0
Sorry the mini sheet option won't work on my work computer. Sample tenancy schedule below. At the moment what you provided works great but I receive different forms of data so where D3 says 'Lease end date' rather than 'Lease expiry date', can I build in flexibility for this to be picked up?

I also then want to do the exact same thing for Tenant Name, Area etc.

Thanks

1676367381457.png
 

Attachments

  • 1676367302655.png
    1676367302655.png
    17.2 KB · Views: 3
Upvote 0
@Anbuselvam
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Sorry the mini sheet option won't work on my work computer.
In what way will it not work? What goes wrong? Could this be the issue?
If that is not the problem, then please describe exactly what goes wrong at at what point in the instructions it happens.
 
Upvote 0
@Anbuselvam
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊


In what way will it not work? What goes wrong? Could this be the issue?
If that is not the problem, then please describe exactly what goes wrong at at what point in the instructions it happens.
It looks like I need Administrator access to add the file to a trusted location. I've raised a ticket but who knows how long it will take them to resolve. Thanks for tip anyway
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,824
Members
449,470
Latest member
Subhash Chand

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