简体   繁体   English

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

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

I have a task to return all the consecutive rows in a particular column. 我有一个任务来返回特定列中的所有连续行。 They will all have data consecutively and the first blank row will signal the end of the data. 它们都将连续具有数据,并且第一空白行将指示数据结束。 I used this to find the column and pass that over to my range, but I keep getting syntax errors. 我用它来查找列并将其传递到我的范围内,但是我一直在遇到语法错误。

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

The goal here is to get the correct range of cells and loop through them, however, I'm not at the loop part as the last line is the one that doesn't work at all. 这里的目标是获得正确的单元格范围并循环通过它们,但是,我不在循环部分,因为最后一行根本不起作用。

So what is the correct syntax for this last line and is there a better way to accomplish what I'm trying to do? 那么最后一行的正确语法是什么,有没有更好的方法来完成我想做的事情?

Try the following which I have tested with a selection of situations. 请尝试以下我在多种情况下测试过的方法。

I have included notes about the changes I have made but come back with questions if anything is unclear. 我提供了有关所做更改的说明,但如果不清楚,还会再提出疑问。

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

When you are using objects in VBA, you need to use the keyword Set when assigning a variable, your first statement should be: 在VBA中使用对象时,在分配变量时需要使用关键字Set ,您的第一条语句应为:

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

By the way, you cannot assign and use a method as Activate . 顺便说一句,您不能将方法分配使用为Activate So I removed it at the end of the statement. 所以我在声明的末尾删除了它。

How about this to get your last row? 如何获得最后一行?

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

This will return the last row before the break/blank cell. 这将返回中断/空白单元格之前的最后一行。

Then set your range using: 然后使用以下命令设置范围:

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