繁体   English   中英

使用Excel VBA仅知道列时如何遍历一系列单元格?

[英]How to loop through a range of cells when only the column is known using Excel VBA?

我有一个任务来返回特定列中的所有连续行。 它们都将连续具有数据,并且第一空白行将指示数据结束。 我用它来查找列并将其传递到我的范围内,但是我一直在遇到语法错误。

Dim Col
Dim found As Boolean
Dim cellRange As Range

    found = Cells.Find(What:="My_Search_Text", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

    'Take that column as the range.
    If (found) Then
        Col = ActiveCell.Column
        cellRange = ActiveSheet.Range(Col, ActiveSheet.Range(Col).End(xlDown)).Select

这里的目标是获得正确的单元格范围并循环通过它们,但是,我不在循环部分,因为最后一行根本不起作用。

那么最后一行的正确语法是什么,有没有更好的方法来完成我想做的事情?

请尝试以下我在多种情况下测试过的方法。

我提供了有关所做更改的说明,但如果不清楚,还会再提出疑问。

Option Explicit
Sub FindRange()

  Dim RangeStart As Range
  Dim RangeEndPlusOne As Range
  Dim RangeEntire As Range

  ' Better to explicitly identify the sheet being searched
  With Sheets("Source")
    ' Do not attempt to activate the target cell.  Instead get it as a range.
    ' Are you sure about "LookAt:=xlPart"?  "LookAt:=xlWhole" might be better
    ' I have explicitly started the search from cell A1 rather than the current
    ' position of the cursor.
    Set RangeStart = .Cells.Find(What:="My_Search_Text", After:=.Range("A1"), _
                                LookIn:=xlValues, LookAt:=xlPart, _
                                SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                MatchCase:=False, SearchFormat:=False)

    If RangeStart Is Nothing Then
      ' My "My_Search_Text" not found
    Else
      ' If the cell below RangeStart is empty, End(xlDown) will jump to
      ' the bottom of the column or the next cell in the column with a value.
      ' Search down the column for an empty cell
      Set RangeEndPlusOne = .Columns(RangeStart.Column).Find(What:="", _
                             After:=RangeStart, _
                             LookIn:=xlValues, LookAt:=xlWhole, _
                             SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                             MatchCase:=False, SearchFormat:=False)
      ' I ought to test for not found but I assume there is a blank cell below
      ' RangeStart

      Set RangeEntire = .Range(RangeStart, RangeEndPlusOne.Offset(-1, 0))

      Debug.Print "Start  " & RangeStart.Address
      Debug.Print "End+1  " & RangeEndPlusOne.Address
      Debug.Print "Entire " & RangeEntire.Address
    End If

  End With

End Sub

在VBA中使用对象时,在分配变量时需要使用关键字Set ,您的第一条语句应为:

Set found = Cells.Find(What:="My_Search_Text", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

顺便说一句,您不能将方法分配使用为Activate 所以我在声明的末尾删除了它。

如何获得最后一行?

LastRow = SheetToCheck.Cells(ActiveCell.Row, ActiveCell.Column).End(xlDown).Row

这将返回中断/空白单元格之前的最后一行。

然后使用以下命令设置范围:

Set cellRange = SheetToCheck.Range(SheetToCheck.Cells(ActiveCell.Row, ActiveCell.Column), SheetToCheck.Cells(LastRow, ActiveCell.Column))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM