使用.Find的功能无法完全完成

时间:2018-07-26 12:10:49

标签: excel excel-vba excel-2010 excel-2007

我有一个需要比较的对象列表。更具体地说,我需要列出此列表中的每个项目:

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)。它将突出显示洋红色项以表明它正在使用该值来查找它,但是该函数将在此处结束,并且找不到该值。

在这里我能做些什么吗?非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

只是以为我会在下面添加我的潜在解决方案版本,实际上只是清理了一下代码,删除了所有.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