Excel VBA查找下一个命令

我遇到了FindNext命令的问题。 这个基于“wash.offset(1,0)”值的代码将试图findsheet1中YAxis2行的第n个实例。 如果“wash.offset(1,0)= 1”,那么它会find第一个实例,它工作正常。 然而,当“wash.offset(1,0)= <> 1”出现问题时,我想通过FindNext实例循环访问“wash.offset(1,0)”的值。 但是,我不断收到错误“无法获取Range类的FindNext属性”

这是这个代码

'Find Row If wash.offset(1, 0) = 1 Then 'wash.offset(1, 1).Select 'Yaxis = ActiveCell.Value ' Set the variable Yaxis to the string value that is located in wash.offset(1, 1) MsgBox "we are in the wash.offset(1,0) = 1 part of the loop" Yaxis = wash.offset(1, 1) 'Set wsThis = ThisWorkbook.Sheets("Sheet1") 'wsThis.Range("E13").Value = instno ' This line of code is definitely needed ... lots of troubleshooting discoevered this ThisWorkbook.Sheets("Sheet1").Select ' Set Yaxis2 = ActiveWorkbook.Sheets("Sheet1").Cells.Find(What:=Yaxis) Yaxis2.Select CellRow = ActiveCell.Row MsgBox "CellRow = " & CellRow Else 'elseif wash.offset(1, 0) <> 1 Then MsgBox "wash.offset(1, 0) = " & wash.offset(1, 0) ' Find first instance of value Set Yaxis2 = ActiveWorkbook.Sheets("Sheet1").Cells.Find(What:=Yaxis) cellAddress = ActiveCell.Address ' This loop cycle through the FindNext function the no. times that value in "wash.offset(1, 0)" is equal to For innerLoop = 1 To wash.offset(1, 0) - 1 ThisWorkbook.Sheets("Sheet1").Select Set Yaxis2 = ActiveWorkbook.Sheets("Sheet1").Cells.FindNext("cellAddress") cellAddress = ActiveCell.Address Next innerLoop Yaxis2.Select CellRow = ActiveCell.Row MsgBox "CellRow = " & CellRow End If 

这是我得到错误的地方

设置Yaxis2 = ActiveWorkbook.Sheets(“Sheet1”)。Cells.FindNext(“cellAddress”)

你的代码有几个问题,但是错误是由用于FindNext()的参数引起的 – 它应该是一个Range对象,而不是String

下一个问题是FindNext()不会激活下一个单元格,所以你的cellAddress总是一样的

说明如何使用FindNext的通用函数:

 Option Explicit Sub findAllValues() Dim foundCell As Range, foundAdr As String With Worksheets(1).Range("A1:A10") Set foundCell = .Find("TestString", LookIn:=xlValues) If Not foundCell Is Nothing Then foundAdr = foundCell.Address Do MsgBox foundCell.Address Set foundCell = .FindNext(foundCell) Loop While Not foundCell Is Nothing And foundCell.Address <> foundAdr End If End With End Sub