You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否有Google Sheets函数可返回包含特定值的所有单元格区域?

实现返回指定匹配值对应单元格区域的自定义函数

先整理你的数据表格:

NameColumn
Andy1
Carol2
Andy3
Carol4
Andy5
Andy6
Andy6

原生Excel或Google Sheets函数无法直接返回这种合并连续/非连续区域的文本格式结果,需要通过自定义函数实现:

Excel 实现方案(VBA自定义函数)

  1. 按下Alt + F11打开VBA编辑器;
  2. 右键点击当前工作簿,选择「插入」→「模块」;
  3. 粘贴以下代码:
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
  1. 返回Excel界面,在任意单元格输入公式:=GetMatchingRanges(A:A,"Andy",1)
    • 第一个参数A:A是要搜索的区域;
    • 第二个参数"Andy"是要匹配的值;
    • 第三个参数1表示返回搜索列右侧第1列的对应区域(比如搜索A列,返回B列)。

执行后会返回B2, B4, B6:B8这样的结果。

Google Sheets 实现方案(Apps Script自定义函数)

  1. 打开你的Google Sheets文档,点击「扩展程序」→「Apps脚本」;
  2. 清空默认代码,粘贴以下脚本:
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(", ");
}
  1. 保存脚本(命名任意,比如RangeMatcher),返回表格界面;
  2. 在单元格输入公式:=GETMATCHINGRANGES(A:A,"Andy",1),即可得到目标格式的区域字符串。

内容的提问来源于stack exchange,提问作者Abdulrahman Mohammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 16:36:07