在电子表格的所有列中搜索唯一值

戴豪斯

我正在为下一个问题的起点而苦苦挣扎。从本质上讲,我需要能够在整个电子表格中搜索唯一的 13 位数字(这是未知的,因此我无法事先引用它),找到对该数字的所有引用,将行复制到新的工作表中,然后然后查找下一个 13 位数字,直到所有不同的 13 位参考都已复制到新工作表中。现在这个数字可能在 A/B 列中,但也可能不在,我们没有在设置模板中提供数据,这就是为什么它需要搜索整个电子表格。谁能给我一个从哪里开始的想法?如果我事先知道数字,我就有一个子程序的基础,但在这种情况下,我们不知道这些数字,只是因为它们在那里。请帮忙?!这是我需要的 VBA 解决方案。样本数据

现在唯一编号可能并不总是在 B 列中,这就是为什么宏需要能够在复制与其相关的所有行之前识别 13 位数字所在的列。我希望这更有意义。

特拉格莫

这是一个可以作为开始的通用 FindAll 函数。您需要指定要搜索的区域(例如.UsedRange)以及您要搜索的内容,它将返回所有匹配单元格的范围。

Function FindAll(What, _
    Optional SearchWhat As Variant, _
    Optional LookIn, _
    Optional LookAt, _
    Optional SearchOrder, _
    Optional SearchDirection As XlSearchDirection = xlNext, _
    Optional MatchCase As Boolean = False, _
    Optional MatchByte, _
    Optional SearchFormat) As Range

    'LookIn can be xlValues or xlFormulas, _
     LookAt can be xlWhole or xlPart, _
     SearchOrder can be xlByRows or xlByColumns, _
     SearchDirection can be xlNext, xlPrevious, _
     MatchCase, MatchByte, and SearchFormat can be True or False. _
     Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
     object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""

    Dim SrcRange As Range
    If IsMissing(SearchWhat) Then
        Set SrcRange = ActiveSheet.UsedRange
    ElseIf TypeOf SearchWhat Is Range Then
        Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
    ElseIf TypeOf SearchWhat Is Worksheet Then
        Set SrcRange = SearchWhat.UsedRange
    Else: SrcRange = ActiveSheet.UsedRange
    End If
    If SrcRange Is Nothing Then Exit Function

    'get the first matching cell in the range first
    With SrcRange.Areas(SrcRange.Areas.Count)
        Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
    End With

    Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
        SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)

    If Not CurrRange Is Nothing Then
        Set FindAll = CurrRange
        Do
            Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
            SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
            If CurrRange Is Nothing Then Exit Do
            If Application.Intersect(FindAll, CurrRange) Is Nothing Then
                Set FindAll = Application.Union(FindAll, CurrRange)
            Else: Exit Do
            End If
        Loop
    End If
End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用JavaScript从Google电子表格中列的所有值中获取排序后的唯一值

谷歌电子表格显示一列中的所有值,但如果数字则替换值

Google表格对电子表格的所有列均具有唯一性

Google 电子表格:将两列中的唯一值合并为一列

使用 Google Apps 脚本和 JavaScript 根据电子表格中的多列获取唯一值

在电子表格中的所有工作表中轻松搜索

根据电子表格中的两列计算唯一值的数量

在所有谷歌电子表格页面中搜索

在电子表格API中按列值搜索行的正确方法是什么

如何使用OpenPyXL列出Excel电子表格中的所有第一行值?

如何从电子表格的第一行返回所有值

垂直复制电子表格中的所有列

按列数对Excel电子表格中的所有行进行排序

建立一个表格,其中包含Google电子表格中每个组的唯一值总计

如何获取Excel电子表格中列的最后一个值?

在Google电子表格中搜索

如何基于一个单元格的所有可能值保存多个电子表格?

我想隐藏Google驱动器电子表格中所有在A列的单元格中没有任何值的行

Google电子表格中单元格的条件唯一数

将QUERY()中的所有行都放入Google电子表格的一个单元格中

使用电子表格中的查询将某列中的值转换为一列?

为什么Excel电子表格中的所有文本列都以引号引起来

检索Google电子表格中所有工作表的值以进行特殊修订

在Google电子表格的一行中搜索两个或多个单词

如何使用带有电子表格 ID、电子表格名称、表格 ID 和值的 golang 在 googlesheet 中插入数据

计算电子表格中的不同值

电子表格中的双列值

将列值从一个电子表格复制到另一电子表格,仅添加必要的行

删除电子表格中所有分组的行/列