Excel VBA查找下一个命令

Excel VBA查找下一个命令,vba,excel,Vba,Excel,FindNext命令有问题。此代码基于“wash.offset(1,0)”的值,将尝试在sheet1中查找YAxis2行的第n个实例。如果“wash.offset(1,0)=1”,那么它将找到第一个实例,这很好。但是,当“wash.offset(1,0)=1”时,就会出现问题,然后我想通过的FindNext实例循环“wash.offset(1,0)”的值。但是,我不断收到错误“无法获取Range类的FindNext属性” 以下是此操作的代码 'Find Row If wash.o

FindNext命令有问题。此代码基于“wash.offset(1,0)”的值,将尝试在sheet1中查找YAxis2行的第n个实例。如果“wash.offset(1,0)=1”,那么它将找到第一个实例,这很好。但是,当“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
“查找行”
如果wash.offset(1,0)=1,则
'wash.offset(1,1)。选择
'Yaxis=ActiveCell.Value
'将变量Yaxis设置为位于wash.offset(1,1)中的字符串值
MsgBox“我们正在清洗中。偏移量(1,0)=循环的1部分”
Yaxis=清洗偏移量(1,1)
'Set wsThis=ThisWorkbook.Sheets(“Sheet1”)
'W此范围(“E13”)。值=仪表号
“这行代码是绝对需要的。。。许多故障排除都发现了这一点
此工作簿。工作表(“表1”)。选择
'
设置Yaxis2=ActiveWorkbook.Sheets(“Sheet1”).Cells.Find(What:=Yaxis)
Yaxis2.选择
CellRow=ActiveCell.Row
MsgBox“CellRow=”&CellRow
Else'elseif wash.offset(1,0)1然后
MsgBox“wash.offset(1,0)=”和wash.offset(1,0)
'查找值的第一个实例
设置Yaxis2=ActiveWorkbook.Sheets(“Sheet1”).Cells.Find(What:=Yaxis)
cellAddress=ActiveCell.Address
'此循环通过FindNext函数循环“wash.offset(1,0)”中的值等于
对于innerLoop=1进行清洗。偏移量(1,0)-1
此工作簿。工作表(“表1”)。选择
Set Yaxis2=ActiveWorkbook.Sheets(“Sheet1”).Cells.FindNext(“cellAddress”)
cellAddress=ActiveCell.Address
下一个内循环
Yaxis2.选择
CellRow=ActiveCell.Row
MsgBox“CellRow=”&CellRow
如果结束
这里就是我得到错误的地方


Set Yaxis2=ActiveWorkbook.Sheets(“Sheet1”).Cells.FindNext(“cellAddress”)

代码中存在多个问题,但错误是由FindNext()使用的参数引起的-它应该是范围对象,而不是字符串

下一个问题是FindNext()将不会激活下一个单元格,因此您的单元格地址始终相同

演示如何使用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
选项显式
子FindAllValue()
Dim foundCell作为范围,FOUNDAR作为字符串
带工作表(1).范围(“A1:A10”)
Set foundCell=.Find(“TestString”,LookIn:=xlValues)
如果不是foundCell,那就什么都不是了
foundAdr=foundCell.Address
做
MsgBox foundCell.Address
设置foundCell=.FindNext(foundCell)
非foundCell时循环为Nothing和foundCell。地址foundAdr
如果结束
以
端接头