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
 
This will search both values, Also you can modify the search value as you want

VBA Code:
Sub ExtractData()

    Dim searchValues As Variant
    Dim searchRange As Range
    Dim resultRange As Range
    Dim foundCell As Range
    Dim lastRow As Long
    Dim outputRow As Long
    
    'Set the search values to look for
    searchValues = Array("Lease expiry date", "Lease end date")
    
    'Set the search range to look for the search values
    Set searchRange = ActiveSheet.UsedRange
    
    'Find the first occurrence of any of the search values in the search range
    For Each searchValue In searchValues
        Set foundCell = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
        If Not foundCell Is Nothing Then
            Exit For
        End If
    Next searchValue
    
    'If any of the search values 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
 
Upvote 0
Solution

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,216,099
Messages
6,128,813
Members
449,469
Latest member
Kingwi11y

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