简体   繁体   中英

If cell matches any others in range copy content to “results” sheet

I want to check if the text in A3 matches the text in any other cells in range A5:A50.

If it does I want to copy the contents of the Y cell (with the same row number as the a cell that matched A3) into the next free cell in the A column in a sheet called results. It needs to also keep the cell colour when it copies the Y cell.

All I could get was putting this in the destination cell

= VLOOKUP(Sheet1!A3, Sheet1$A$5:$Y50, 25, TRUE) 

That doesn't work as I need to run it over multiple sheets all with different names.

with activesheet
   for each c in .range("A5:A50").cells
      if c.value = .range("A3").value then
         c.offset(0,24).copy _
              worksheets("results").cells(rows.count,1).end(xlup).offset(1,0) 
      end if
   next c
end with

You can employ a dynamic 3d-Vlookup technique by listing the sheetnames to search in a list somewhere, naming that range of cells MySheets and then use a construct like this:

=VLOOKUP(A3, INDIRECT("'" & INDEX(MySheets, MATCH(1, COUNTIF(INDIRECT("'" & MySheets &"'!A1:A50"), A3), 0)) & "'!A:B"), 2, 0)

There's a sample workbook on my site demonstrating this.

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