【问题标题】:Search entire worksheet for a cell contain a string of text (formula)在整个工作表中搜索包含一串文本(公式)的单元格
【发布时间】:2018-07-31 08:14:32
【问题描述】:

我有一个 Excel 文件,开头有一个“摘要”选项卡,然后是几个包含每周信息的选项卡。随着周数的进展,我可能会在周表中添加额外的行,这意味着总数并不总是在每张表的同一单元格中。

我正在寻找一个公式,它将在指定的每周工作表中搜索“总计”一词并返回它的地址,然后我可以将其构建到其他公式中,以便汇总表将填充正确的值,无论是否已添加其他行。

【问题讨论】:

    标签: excel vba formula


    【解决方案1】:

    您好,您的问题应该始终包含您当前的工作,最好还包含一些代码。但鉴于这是一个相当基本的问题,而且你是新来的,我决定回答它


    除此之外......:

    Option Explicit
    Private Function get_total () As Range
        Dim ws As Worksheet: Set ws = Sheets("Your Sheet Name") 'set your own
        Dim lc as Long
        lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
        ' determines last column with data
    
        'some other useful variables
        Dim lr as Long 'last row
        Dim temp as Range 'find result
        Dim i as Long ' loop index
    
        For i = 1 to lc
           lr = ws.Cells(Rows.Count, i).End(xlUp).Row
           Set temp = ws.Range(Cells(1, i), Cells(lr, i)).Find("Grand Total", lookin:=xlValues)
           If Not temp Is Nothing Then 'if we found grand total, return it
                Set get_total = temp 'return range of result
           End If
         Next i
    
       Exit Function 'if no Range found, exit function ("return;")
    
    End Function
    

    从技术上讲,这就是答案,但为了实际使用,您可能希望将其可视化

    因此,使用此函数,您将遍历所有列,并在找到总计时停止,或者如果未找到任何列,“return”将无效

    显然,这只是一个函数,所以如果你想打印出结果,你可以创建一个这样的过程:

    Private Sub print_grand_total() 
      Dim res as Range
      Set res = get_total
      If Not res Is Nothing Then
         MsgBox("Found in Cell[" & res.row & "," & res.Column & "]")
      Else
         MsgBox("Grand Total not found!")
      End If
    End Sub
    

    所以,我设置了一个示例表:

    程序运行后返回结果,成功:

    注意: 小实现前瞻:这在第一行为空的情况下不起作用(因为它无法正确检测最后一列)。根据数据范围的开始位置编辑lc 代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2022-07-20
      • 1970-01-01
      相关资源
      最近更新 更多