是否有Google Sheets函数可返回包含特定值的所有单元格区域?
实现返回指定匹配值对应单元格区域的自定义函数
先整理你的数据表格:
| Name | Column |
|---|---|
| Andy | 1 |
| Carol | 2 |
| Andy | 3 |
| Carol | 4 |
| Andy | 5 |
| Andy | 6 |
| Andy | 6 |
原生Excel或Google Sheets函数无法直接返回这种合并连续/非连续区域的文本格式结果,需要通过自定义函数实现:
Excel 实现方案(VBA自定义函数)
- 按下
Alt + F11打开VBA编辑器; - 右键点击当前工作簿,选择「插入」→「模块」;
- 粘贴以下代码:
Function GetMatchingRanges(searchRange As Range, matchValue As Variant, returnOffsetCol As Integer) As String Dim cell As Range Dim result As String Dim startRow As Long Dim inRange As Boolean inRange = False result = "" For Each cell In searchRange ' 跳过空单元格(可根据需求删除此行) If cell.Value = "" Then GoTo NextCell If cell.Value = matchValue Then If Not inRange Then startRow = cell.Row inRange = True End If Else If inRange Then ' 拼接区域字符串 If result <> "" Then result = result & ", " If startRow = cell.Row - 1 Then result = result & Cells(startRow, searchRange.Column + returnOffsetCol).Address(False, False) Else result = result & Cells(startRow, searchRange.Column + returnOffsetCol).Address(False, False) & ":" & Cells(cell.Row - 1, searchRange.Column + returnOffsetCol).Address(False, False) End If inRange = False End If End If NextCell: Next cell ' 处理最后一段连续匹配的区域 If inRange Then If result <> "" Then result = result & ", " Dim lastRow As Long lastRow = searchRange.Rows(searchRange.Rows.Count).Row If startRow = lastRow Then result = result & Cells(startRow, searchRange.Column + returnOffsetCol).Address(False, False) Else result = result & Cells(startRow, searchRange.Column + returnOffsetCol).Address(False, False) & ":" & Cells(lastRow, searchRange.Column + returnOffsetCol).Address(False, False) End If End If GetMatchingRanges = result End Function
- 返回Excel界面,在任意单元格输入公式:
=GetMatchingRanges(A:A,"Andy",1)- 第一个参数
A:A是要搜索的区域; - 第二个参数
"Andy"是要匹配的值; - 第三个参数
1表示返回搜索列右侧第1列的对应区域(比如搜索A列,返回B列)。
- 第一个参数
执行后会返回B2, B4, B6:B8这样的结果。
Google Sheets 实现方案(Apps Script自定义函数)
- 打开你的Google Sheets文档,点击「扩展程序」→「Apps脚本」;
- 清空默认代码,粘贴以下脚本:
function GETMATCHINGRANGES(searchRange, matchValue, returnOffsetCol) { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const range = sheet.getRange(searchRange); const data = range.getValues(); const startRowOfRange = range.getRow(); const searchCol = range.getColumn(); let result = []; let startRow = null; for (let i = 0; i < data.length; i++) { const currentRow = startRowOfRange + i; const cellValue = data[i][0]; if (cellValue === matchValue) { if (startRow === null) { startRow = currentRow; } } else { if (startRow !== null) { const endRow = currentRow - 1; const targetCol = searchCol + returnOffsetCol; const colLetter = String.fromCharCode(64 + targetCol); if (startRow === endRow) { result.push(`${colLetter}${startRow}`); } else { result.push(`${colLetter}${startRow}:${colLetter}${endRow}`); } startRow = null; } } } // 处理最后一段连续匹配区域 if (startRow !== null) { const endRow = range.getLastRow(); const targetCol = searchCol + returnOffsetCol; const colLetter = String.fromCharCode(64 + targetCol); if (startRow === endRow) { result.push(`${colLetter}${startRow}`); } else { result.push(`${colLetter}${startRow}:${colLetter}${endRow}`); } } return result.join(", "); }
- 保存脚本(命名任意,比如
RangeMatcher),返回表格界面; - 在单元格输入公式:
=GETMATCHINGRANGES(A:A,"Andy",1),即可得到目标格式的区域字符串。
内容的提问来源于stack exchange,提问作者Abdulrahman Mohammed
相关产品推荐
相关产品推荐

