【发布时间】: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