【问题标题】:create range with find and lastrow使用 find 和 lastrow 创建范围
【发布时间】:2017-01-27 23:04:42
【问题描述】:

这可要了我的命。 我想在第一列中找到一个包含例如单词“alex”的单元格。 我们称这个单元为 cell-alex。

我们的想法是: 范围(细胞亚历克斯,细胞(lastrow,1))。

我知道如何到达最后一行,但是 cell-alex 正在杀死我(我认为 excel 看不到它。它总是选择从 A1 到最后一行的范围)。

这是一段代码:

Cells.Find(What:="alex", after:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
            , SearchFormat:=True).Select        
    Set sht = Worksheets(sheetbr)
    lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    Range(ActiveCell, Cells(lastrow, 13)).Select

【问题讨论】:

    标签: vba excel find range


    【解决方案1】:

    如果您确定“Alex”在第 1 列,请使用:

    With Worksheets(sheetbr)
        .Range(.Columns(1).Find(What:="alex", after:=.Cells(.Rows.Count, 1), LookIn:=xlFormulas, LookAt:=xlPart, _
                                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, _
                                SearchFormat:=True), _
               .Cells(.Rows.Count, "A").End(xlUp)).Select
    End With
    

    否则使用这个:

    Dim f As Range
    
    With Worksheets(sheetbr)
        Set f = .Columns(1).Find(What:="alex", after:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
                xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
                , SearchFormat:=True)
        If Not f Is Nothing Then .Range(f, .Cells(.Rows.Count, "A").End(xlUp)).Select
    End With
    

    【讨论】:

    • @PeterPeanut,你通过了吗?
    • 它正在工作。我不知道为什么,但不知何故这是lastrow的问题。我编辑了 Set sht = ThisWorkbook.Worksheets(1)。谢谢大家。
    【解决方案2】:

    我认为问题在于 LookIn:=xlFormulas。尝试更改为 LookIn:=xlValues

    【讨论】:

      【解决方案3】:

      好吧,我已经想通了。 我没有告诉你一切。 我从导入另一个文档开始我的代码。 当我在编写代码时,当您提到 ActiveCell 时,它就开始与另一本书一起工作。 我通过将导入的电子表格中的数据复制到原始电子表格(book1)来解决它。 其余的很容易。就是这样:

      Set sht = ThisWorkbook.Worksheets(1)
      lastrow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'to find the lastRow
      Cells.Find(What:="GuV 7", after:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
                  xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
                  , SearchFormat:=True).Activate
      Range(ActiveCell, Cells(lastrow, 13)).Select 'I need columns 1-13
      

      再次感谢各位。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2014-02-24
      • 1970-01-01
      相关资源
      最近更新 更多