2015-08-13 2 views
1

У меня возникла проблема с командой FindNext. Этот код, основанный на значении «wash.offset (1, 0)», попытается найти n-й экземпляр строки YAxis2 в листе1. Если «wash.offset (1, 0) = 1», то он найдет первый экземпляр, который отлично работает. Однако проблема возникает, когда «wash.offset (1, 0) = <> 1», тогда я хочу прокрутить экземпляр FindNext для значения «wash.offset (1,0)». Тем не менее, я постоянно получаю ошибку «Невозможно получить свойство FindNext класса Range»Excel VBA Найти следующую команду

Вот код этого

'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 

Вот где я получаю ошибку

Set Yaxis2 = ActiveWorkbook.Sheets ("Лист1"). Cells.FindNext ("cellAddress")

ответ

1

Есть Seve ral в вашем коде, но ошибка вызвана параметром, используемым для 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