【问题标题】:This Function Using .Find Can't Completely Finish使用 .Find 的这个函数不能完全完成
【发布时间】:2019-01-03 10:24:14
【问题描述】:

我有一个需要与另一个列表进行比较的对象列表;更具体地说,我需要获取此列表中的每个单独项目:

GAC-CR-02918
GVII-GER-2166
GVII-G600-GSN-552001
739124.003
GVII-G600-GSN-551002
GVII-G600-GSN-533001
735330.001
735750.001
GVII-GER-2309
730000.001
GVII-GER-0775

我需要在更大的项目列表中找到它们。我知道这些都在较大的列表中,因为我已经通过搜索和替换工具手动找到了它们。

这是我的代码:

Function g600BurndownUser()
  With Workbooks.Open(fileName:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master  (plus GSNs) 3Q Rev.xlsx", ReadOnly:=True).Worksheets(1).Range("A1:T2500")
    .AutoFilter
    .AutoFilter 20, "y"
    .AutoFilter 13, ""
    .Rows.Sort Key1:=Columns("A"), Order1:=xlAscending
    .Rows.Sort Key1:=Columns("E"), Order1:=xlAscending
    .Copy
  End With
  ThisWorkbook.Worksheets(2).Activate
  ActiveSheet.Range("A1:N2500").NumberFormat = "@"
  Range("A1:A2500").PasteSpecial
  ThisWorkbook.Worksheets(1).Activate
  Dim counter As Integer, countyBoi(1 To 100), textValue As String, offsetValue As Integer, startingPoint As Range
  Set startingPoint = Range("A1:N2500").Find("Report Number"): offsetValue = 1
  For counter = LBound(countyBoi) To UBound(countyBoi)
    If startingPoint.Offset(offsetValue, 0).Value = "*note: only over-due G600 Cert Reports are included on this list" Then
      Exit For
    End If
    If startingPoint.Offset(offsetValue, 0).Value <> "*note: only over-due G600 Cert Reports are included on this list" Then
      ThisWorkbook.Worksheets(1).Activate
      Range("A1:A2500").Select
      Selection.NumberFormat = "@"
      startingPoint.Offset(offsetValue, 0).Interior.Color = RGB(255, 0, 255)
      textValue = startingPoint.Offset(offsetValue, 0).Value
      ThisWorkbook.Worksheets(2).Range("A1:A2500").Find(textValue, LookIn:=xlValues).Interior.Color = RGB(0, 255, 255)
      offsetValue = offsetValue + 1
    End If
  Next counter
End Function

我已经知道该代码有效,因为它以洋红色突出显示在较小列表中遍历的每个项目,并以青色突出显示在较大列表中找到的项目的副本。但是,我的问题在于以 7 开头的较小列表中的每个项目(即 739124.003)。它将突出显示项目洋红色以表明它正在使用该值来查找它,但该函数将在那里结束,并且找不到该值。

有什么我可以在这里做的吗?非常感谢您的帮助。

【问题讨论】:

  • 您可以尝试将您的.Find(textvalue, LookIn:=xlValues) 修改为.Find(textvalue, LookIn:=xlValues, LookAt:=xlWhole),看看您的结果是否更符合您的预期?
  • 我会尝试一下,然后告诉你会发生什么。非常感谢!
  • 另外,我相信您增加偏移值的行可能应该被丢弃,因为随着循环的进行,您会继续以更高的值偏移,删除行 offsetValue = offsetValue + 1 我不确定你在哪里将值分配给countyBoi,但我想如果您的代码确实到达了 For 循环,这不是问题。
  • 嘿,您的建议完美无缺,非常感谢您的建议。
  • 不用担心,很高兴它有帮助! :)

标签: excel vba excel-2010 excel-2007


【解决方案1】:

只是想我会在下面添加我的潜在解决方案版本,实际上只是稍微清理了代码,删除了任何 .Activate / .Select 并使用 ElseIF 更改了 If 语句,请看看它可能会出现派上用场:

Function g600BurndownUser()
Dim counter As Integer, countyBoi(1 To 100), textValue As String, offsetValue As Integer, startingPoint As Range
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(1)
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets(2)

    With Workbooks.Open(Filename:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master  (plus GSNs) 3Q Rev.xlsx", ReadOnly:=True).Worksheets(1).Range("A1:T2500")
      .AutoFilter
      .AutoFilter 20, "y"
      .AutoFilter 13, ""
      .Rows.Sort Key1:=Columns("A"), Order1:=xlAscending
      .Rows.Sort Key1:=Columns("E"), Order1:=xlAscending
      .Copy
    End With

    ws2.Range("A1:N2500").NumberFormat = "@"
    ws2.Range("A1:A2500").PasteSpecial

    Set startingPoint = ws.Range("A1:N2500").Find("Report Number")
    offsetValue = 1

    For counter = LBound(countyBoi) To UBound(countyBoi)
          If startingPoint.Offset(offsetValue, 0).Value = "*note: only over-due G600 Cert Reports are included on this list" Then
              Exit For
          ElseIf startingPoint.Offset(offsetValue, 0).Value <> "*note: only over-due G600 Cert Reports are included on this list" Then
              ws.Range("A1:A2500").NumberFormat = "@"
              startingPoint.Offset(offsetValue, 0).Interior.Color = RGB(255, 0, 255)
              textValue = startingPoint.Offset(offsetValue, 0).Value
              ws2.Range("A1:A2500").Find(textValue, LookIn:=xlValues, Lookat:=xlWhole).Interior.Color = RGB(0, 255, 255)
          End If
    Next counter
End Function

【讨论】:

  • 感谢您的回答;与来自 Xabier 的 cmets 一起,我还发现由于某些奇怪的原因,如果列太短并且它会删除数字,它只会搜索可见的内容,所以这是我的问题的一部分。也感谢清理代码!
猜你喜欢
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多