[英]Excel: Check if cell string value exists in column, and get all cell references to that string
I suspect this may be a job for VBA, which is beyond my abilities. 我怀疑这可能是VBA的工作,这超出了我的能力范围。 But here's the scenario:
但是这是场景:
Column A in Sheet 1 (CAS1) contains x rows of text values 工作表1(CAS1)中的A列包含x行文本值
Column A in Sheet 2 (CAS2) contains x rows of text values 工作表2(CAS2)中的A列包含x行文本值
Part A - For each row value in CAS1, I need to know if the string is contained in any of the cells in CAS2. A部分-对于CAS1中的每个行值,我需要知道该字符串是否包含在CAS2中的任何单元格中。 Not exact match, the string can be only part of the searched cells.
不完全匹配,字符串只能是搜索到的单元格的一部分。
Part B - I need to know the cell value of each cell in CAS2 that contains the CAS1 value (if they do exist, they can be listed in the cells adjacent to the cell being searched in CAS1). B部分-我需要知道CAS2中包含CAS1值的每个单元格的单元格值(如果它们确实存在,则可以在与CAS1中正在搜索的单元格相邻的单元格中列出它们)。
I've tried the following to attempt Part A, all to no avail: 我尝试了以下尝试A部分的尝试,但都无济于事:
vlookup(A1,sheet2!A:A,1,false)
NOT(ISNA(MATCH(A1,sheet2!A:A,0)))
ISNUMBER(MATCH(A1,sheet2!A:A,0))
COUNTIF(sheet2!A:A,A1)>0
IF(ISERROR(MATCH(A1,sheet2!A:A, 0)), "No Match", "Match")
I know some of the cell values in CAS2 contain the cell values in CAS1, so I don't know why they return false or No Match. 我知道CAS2中的某些单元格值包含CAS1中的单元格值,所以我不知道为什么它们返回false或No Match。 I suspect it may be down to the nature of the text content.
我怀疑这可能取决于文本内容的性质。 So here's some sample data:
所以这是一些示例数据:
CAS1 CAS1
LQ056
RV007H
RV008
RV009H
TSN304
TSN305
CAS2 CAS2
RV009-satin-nickel-CO.jpg
STR314.jpg
STR315.jpg
HCY001.jpg
RV008-oval-rad-CO.jpg
HCY001-BRAC006.jpg
Any help would be appreciated. 任何帮助,将不胜感激。
This problem can be faced through VBA (at least, I imagine the VBA solution much more easily than the possible Excel one). 通过VBA可以解决此问题(至少,我认为VBA解决方案比可能的Excel解决方案容易得多)。 You need a macro that, for each row in CAS1, search the content in each row of CAS2 and returns you the address.
您需要一个宏,该宏针对CAS1中的每一行,搜索CAS2中每一行的内容并返回地址。
For Each cell In Sheets("CAS1").Range("A1:A" & Sheets("CAS1").Range("A1").End(xlDown).Row) '<-- check each cell of the range A1:A? of sheet CAS1 (adapt "A" and "1" if they're different)
recFound = 0 '<-- count how many findings there are
For Each cell2 In Sheets("CAS2").Range("A1:A" & Sheets("CAS2").Range("A1").End(xlDown).Row) '<-- check in each cell of the range A1:A? of sheet CAS2 (adapt "A" and "1" if they're different)
If InStr(cell2.Value, cell.Value) <> 0 Then '<-- if the value in cell is contained in the value in cell2..
recFound = recFound + 1 '<-- account the new finding
cell.Offset(0, recFound) = Split(cell2.Address, "$")(1) & Split(cell2.Address, "$")(2) '<--write the address on the right of the currently searched cell
End If
Next cell2
Next cell
All the above should be enclosed in a macro, eg Sub makeMySearch()
, that should be run to get the results. 以上所有内容均应包含在宏中,例如
Sub makeMySearch()
,应运行该宏以获取结果。 As commented in my code, I'm assuming that data are in A1:A?
如我的代码中所述,我假设数据在
A1:A?
of both sheets; 在两张纸上; but they of course might be, for example, in
B5:B?
但是它们当然可能例如在
B5:B?
of the sheet 1 and in C7:C?
表1和
C7:C?
of the sheet 2. You need clearly to adapt the code to your current data. 表2的内容。您需要清楚地使代码适应当前数据。
There's no need for VBA. 不需要VBA。 Some simple array-formulas can do the job.
一些简单的数组公式可以完成这项工作。
To see if the entry in CAS1 is present in CAS2: 要查看CAS2中是否存在CAS1中的条目:
=OR(ISNUMBER(SEARCH(A2,CAS2_)))
will return TRUE or FALSE. 将返回TRUE或FALSE。 BUT this formula has to be entered by holding down CTRL-SHIFT while hitting ENTER If you do this correctly, Excel will place braces {...} around the formula that you can see in the formula bar.
但是必须在按下ENTER 键的同时按住CTRL-SHIFT键输入此公式。如果正确执行此操作,Excel会将括号{...}放在可以在公式栏中看到的公式周围。
The SEARCH function returns an array of results, which will be either the #VALUE! SEARCH函数返回一个结果数组,该数组将为#VALUE! error, or a number.
错误或数字。
In order to return the address, the following array-formula can be entered adjacent to a cell in CAS1: 为了返回地址,可以在CAS1中的一个单元格附近输入以下数组公式 :
=IFERROR(ADDRESS(LARGE(ISNUMBER(SEARCH($A2,CAS2_))*ROW(CAS2_),COLUMNS($A:A)),1),"")
Fill right for the maximum number of addresses possible, then select the group and fill down. 向右填写以获取最大可能的地址,然后选择组并填写。
In this case, the array being returned is a string of either 0's, or 1 * the row number (ie the row number). 在这种情况下,返回的数组是一个0或1 *行号(即行号)的字符串。 I assumend the data in CAS2 was in column A, but you can change the column number if needed (or even compute it if necessary, by replacing the
1
in the ADDRESS
function with COLUMN(CAS2_)
) 我假设CAS2中的数据在A列中,但是您可以根据需要更改列号(甚至可以根据需要进行计算,方法是将
ADDRESS
函数中的1
替换为COLUMN(CAS2_)
)
CAS1_ and CAS2_ are either named ranges, or absolute range references to the two text groups. CAS1_和CAS2_是两个文本组的命名范围或绝对范围引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.