我遇到了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
这是我收到错误的地方
设置Yaxis2 = ActiveWorkbook.Sheets(“Sheet1”)。Cells.FindNext(“cellAddress”)
答案 0 :(得分:1)
您的代码中存在多个问题,但错误是由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