【问题标题】:how to search for two value in range collectivly in VBA Excel如何在 VBA Excel 中集体搜索范围内的两个值
【发布时间】:2019-04-22 20:22:29
【问题描述】:

代码有一个搜索顺序,它的作用如下:

它获取 ws.range A 中的每个值并在另一个名为 wp.range A 的工作表中查找它。如果 ws.range 中的特定值 x 例如 A3 在 wp.range A 中未找到,则工作表中的下一个搜索顺序“ws”是整个​​范围B中要在工作表“wp”中搜索的下一个范围B3(与值x相同的行)中的值y。如果不是,则认为范围C(工作表ws)中的下一个值是在 C 列的另一张工作表 wp 中找到。

现在,如果没有找到范围 A 和范围 B 中的值,则不仅范围 C 中的值,而且范围 C 和 D 中的值都将被共同进行比较。但是代码:

 Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 1), 
 Cells(number_of_ru, I + 1)).Find(cell.Offset(0, I).Value) And
 wp.Sheets("ABC").Range(Cells(ru, I + 2), 
 Cells(number_of_ru, I + 2)).Find(cell.Offset(0, I + 1).Value)

给我错误信息:“类型不兼容”

Dim cell As Range
r = 2
number_of_r = 30
ru = 1
number_of_ru = 30


For Each cell In ws.Range("A" & r & ":A" & number_of_r)
For I = 0 To 2 'Column Offset form A to C
  Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 
1), Cells(number_of_ru, I + 1)).Find(cell.Offset(0, I).Value)
If Not foundValue Is Nothing Then
    wp.Sheets("ABC").Rows(foundValue.Row).Columns("E:AB").Copy
    ws.Range("G" & cell.Row).PasteSpecial
    GoTo nextCell
End If
Next I
     Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 1), 
    Cells(number_of_ru, I + 1)).Find(cell.Offset(0, I).Value) And 
    wp.Sheets("ABC").Range(Cells(ru, I + 2), Cells(number_of_ru, I + 
    2)).Find(cell.Offset(0, I + 1).Value)
    If Not foundValue Is Nothing Then
        wp.Sheets("ABC").Rows(foundValue.Row).Columns("E:AB").Copy
        ws.Range("G" & cell.Row).PasteSpecial
        GoTo nextCell
    Else: MsgBox "Problem"
    End If
nextCell:
Next cell

End sub

【问题讨论】:

    标签: loops for-loop find range rows


    【解决方案1】:
     For Each cell In ws.Range("A" & r & ":A" & number_of_r)
     For I = 0 To 2 'Column Offset form A to C
     Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 
     1), Cells(number_of_ru, I + 1)).Find(cell.Offset(0, I).Value)
    If Not foundValue Is Nothing Then
    wp.Sheets("ABC").Rows(foundValue.Row).Columns("E:AB").Copy
    ws.Range("G" & cell.Row).PasteSpecial
    GoTo nextCell
    End If
    Next I
     Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 1), 
    Cells(number_of_ru, I + 1)).Find(cell.Offset(0, I).Value) 
      If Not foundValue Is Nothing Then
     Set foundValue = wp.Sheets("ABC").Range(Cells(ru, I + 2), Cells(number_of_ru, I + 
    2)).Find(cell.Offset(0, I + 2).Value)
    If Not foundValue Is Nothing Then
        wp.Sheets("ABC").Rows(foundValue.Row).Columns("E:AB").Copy
        ws.Range("G" & cell.Row).PasteSpecial
        GoTo nextCell
       End If 
    Else: MsgBox "Problem"
    End If
    nextCell:
    Next cell
    
    End sub
    

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多