简体   繁体   中英

Lookup in all the sheets for specific text and if match then write value of same row but different column value to master sheet

  • check over all sheets
  • If specific text (eg, yes) is found in the fifth row of a sheet, get data from that row (but different column) and write data to a master sheet How can I do that?

I found below code relatively useful for me but, not 100% matching with my requirement. please help me on this.

Problem in this code is:- i have specific text(yes) in specific range(e6:e16). What I want is to check for only yes word. If found then write value of column A & row, in which yes word found, into master sheet and check it till last sheet.

Sub SeachSheets()

    Dim FirstAddress As String, WhatFor As String
    Dim Cell As Range, Sheet As Worksheet

    WhatFor = InputBox("What are you looking for?", "Search Criteria")
    If WhatFor = Empty Then Exit Sub

    For Each Sheet In Sheets
        If Sheet.Name <> "SEARCH" Then
            With Sheet.Columns(1)
                Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlPart)
                If Not Cell Is Nothing Then
                    FirstAddress = Cell.Address
                    Do
                        Cell.EntireRow.Copy _
                        Destination:=Sheets("SEARCH").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                        Set Cell = .FindNext(Cell)
                    Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
                End If
            End With
        End If
    Next Sheet

    Set Cell = Nothing
End Sub

Any help will be appreciated. Thank you.

For one sheet, this is easy to accomplish with a formula alone, eg

= INDEX(1:1,MATCH("yes",5:5,0))

This finds the first instance of yes in the 5th row of the current sheet, and returns the value in 1st row and in the same column.

One option is to have this formula above somewhere on each sheet in your workbook as a "helper cell" (eg cell Z99 ) and then have a formula somewhere on your master sheet to check all of these helper cells, eg

= IFERROR(Sheet1!Z99,IFERROR(Sheet2!Z99,IFERROR(Sheet3!Z99,IFERROR(...,"no match"))))

Of course, also possible without helper cells at all, but the formula just gets messy:

= IFERROR(INDEX(Sheet1!1:1,MATCH("yes",Sheet1!5:5,0)),
  IFERROR(INDEX(Sheet2!1:1,MATCH("yes",Sheet2!5:5,0)),
  IFERROR(INDEX(Sheet3!1:1,MATCH("yes",Sheet3!5:5,0)),
  IFERROR(...,"no match"))))

If you want a way without explicitly calling each sheet, then VBA is probably required. Just posting a solution without VBA to see if this will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM