[英]Check if an excel cell value exists on multiple other sheets - and if so return the name of the sheet it exists on in another column
The first two sheets of my workbook each contain a single column, column A, of approximately 2000 values. 我的工作簿的前两页各包含一个单独的列,即A列,其值约为2000。 In addition to these two sheets I have 42 other sheets that each contain anywhere from 20-1500 values, also in a single column A.
除了这两张纸之外,我还有42张其他纸,每张纸都包含20-1500个值,也都在单列A中。
For each of the ~2000 values in column A of sheets 1 and 2 I am trying to check if those values exist in any of the other 42 sheets. 对于工作表1和2的A列中的〜2000值中的每个值,我试图检查这些值是否存在于其他42个工作表中。 If so, I'd like the name of the sheet they exist in to show up in column B.
如果是这样,我希望它们存在的工作表的名称显示在B列中。
So: 所以:
IF sheet1.A1.value EXISTS IN sheet3.A:A
RETURN sheet3.name
ELSE IF sheet1.A1.value EXISTS IN sheet4.A:A
RETURN sheet4.name
etc... 等等...
Sub test()
Dim ws As Worksheet
Dim i As Integer
Dim fRange As Range
'Perform search for Sheet1
Set ws = Sheets("Sheet1")
i = 3
While i <= ActiveWorkbook.Sheets.Count
ws.Select
Set fRange = Range("A1")
fRange.Select
While fRange.Value <> ""
Sheets(i).Select
Range("A1").Select
While ActiveCell.Value <> ""
If ActiveCell.Value = fRange.Value Then
fRange.Offset(0, 1).Value = Sheets(i).Name
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(1, 0).Select
End If
Wend
Set fRange = fRange.Offset(1, 0)
Wend
i = i + 1
Wend
'Perform search for Sheet2
Set ws = Sheets("Sheet2")
i = 3
While i <= ActiveWorkbook.Sheets.Count
ws.Select
Set fRange = Range("A1")
fRange.Select
While fRange.Value <> ""
Sheets(i).Select
Range("A1").Select
While ActiveCell.Value <> ""
If ActiveCell.Value = fRange.Value Then
fRange.Offset(0, 1).Value = Sheets(i).Name
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(1, 0).Select
End If
Wend
Set fRange = fRange.Offset(1, 0)
Wend
i = i + 1
Wend
End Sub
Use a For Each loop on the worksheets collection to cycle through the worksheets. 在工作表集合上使用For Each循环在工作表之间循环。 Then use the Range.Find method to check if the cell is found on the worksheet.
然后使用Range.Find方法检查是否在工作表上找到该单元格。 If it is, just write the sheet name to your cover sheet.
如果是这样,只需将工作表名称写到封面。 You can concatenate to one cell, or use a counter to move to the next available column.
您可以连接到一个单元格,或使用计数器移动到下一个可用列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.