简体   繁体   English

编写一个for循环以查找从活动行到最后一个工作表末尾的值

[英]Write a for loop to find a value from the active row to the end of last worksheet

I have a workbook which contains 4 worksheets, each for one quarter in 2019. 我有一个工作簿,其中包含4个工作表,每个工作表在2019年占四分之一。
I would like to write a function to check, whether the input of active cell is a duplicate from the active cell row to the end of the last worksheet. 我想编写一个函数来检查活动单元格的输入是否是从活动单元格行到最后一个工作表末尾的重复项。

Function checkDuplicate(ByVal ChangedCell As Range) As Boolean
     TelNo = ChangedCell.Value

     Range(ActiveCell.Address).Name = "StartCell"
     For i = ActiveSheet.Index To ActiveWorkbook.Worksheets.count
        Worksheets(i).Activate
        With Worksheets(i)
            Set rng = Cells.Find(What:=TelNo, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        End With
Next i
Application.Goto "StartCell"
If rng Is Nothing Then
    checkDuplicate = False
Else
    checkDuplicate = True
End If

However, it seems that it cannot even find the duplicated values in the row below the active cell. 但是,似乎它甚至无法在活动单元格下面的行中找到重复的值。

As it stands your loop is useless. 就目前而言,您的循环是无用的。 You could do this with the exact same result: Set rng = Worksheets(4).Cells.Find(What:=TelNo, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) 您可以使用完全相同的结果执行此操作: Set rng = Worksheets(4).Cells.Find(What:=TelNo, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

Try this: 尝试这个:

Function checkDuplicate(ByVal ChangedCell As Range) As Boolean
  TelNo = ChangedCell.Value

  Range(ActiveCell.Address).Name = "StartCell"
  Set rng = ActiveSheet.Cells.Find(What:=TelNo, After:=changedCell LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
  If Not rng Is Nothing And rng.Address <> ChangedCell.Address Then checkDuplicate = True

  If ActiveSheet.Index < ActiveWorkbook.Worksheets.Count Then
     For i = ActiveSheet.Index + 1 To ActiveWorkbook.Worksheets.count
        With Worksheets(i)
           Set rng = .Cells.Find(What:=TelNo, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
           If Not rng Is Nothing Then checkDuplicate = True
        End With
     Next i
   End If
End Function

Notice I added a . 注意,我添加了一个. before the range.find call. range.find调用之前。 That is for the With Worksheets(i) to take effect. 这是因为With Worksheets(i)才能生效。 This is also the reason why I removed the Worksheets(i).Activate , since it is unnecessary when fully qualifying the sheets. 这也是我删除Worksheets(i).Activate ,因为在完全限定工作表时没有必要。
Another thing to note: Range(ActiveCell.Address).Name = "StartCell" is implicitly called on ActiveSheet . 需要注意的另一件事: Range(ActiveCell.Address).Name = "StartCell"ActiveSheet上隐式调用。

You should avoid using ActiveSomething in your code in general, as it can cause a great deal of errors. 通常,应避免在代码中使用ActiveSomething ,因为它会导致大量错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM