1

使用For EachandFind方法,即使范围是多列多行,我也可以在范围内查找单元格。但是我怎样才能在一个范围内寻找一组单元格呢?

例如,我有一本参考书,其行条目填充第 2-7 行并且有两列宽。我需要确定它们是否与我的目标书中的行条目(也从第 2 行开始,并且是两列宽)完全匹配。如果找到匹配项,则将参考文件中的第 # 行粘贴到第 5 列。否则,粘贴到新条目中。这是到目前为止的代码:

Dim NewFile as Workbook  'defined as the location of the reference book  
Dim NewSht as Worksheet  'defined as the sheet in NewFile  
Dim ThisSht as Worksheet  'defined as the sheet in the target book
Dim NewName as Range  
Dim LastEntry as Range  
Dim OldNameRange as Range  
Dim EntryMatch as Range  
For Each NewName In NewSht.Range(NewSht.Cells(2, 1), NewSht.Cells(Rows.Count, 2).End(xlUp)) 'loops thru each set of entry labels in new sheet  
  Set LastEntry = ThisSht.Cells(Rows.Count, 2).End(xlUp)
  Set OldNameRange = ThisSht.Range(ThisSht.Cells(2, 1), LastEntry)
  Set EntryMatch = OldNameRange.Find(NewName.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)  
  If Not EntryMatch Is Nothing Then  
    ThisSht.Cells(EntryMatch.Row, 5).Value = NewName.Row  
  Else  
    ThisSht.Cells(LastEntry.Row + 1, 1).Resize(1, 2).Value = NewSht.Range(NewSht.Cells(NewName.Row, 1), NewName.Cells(NewName.Row, 2)).Value  
    ThisSht.Cells(LastEntry.Row + 1, 3).Value = NewName.Row  
  End If  
Next

现在,它只是逐个单元格地比较。谁能帮我将一组单元格(每行条目)与 中的每组单元格进行比较OldNameRange?谢谢!

4

0 回答 0