2016-08-02 72 views
-1

我有一个场景,我必须在工作表1中查找两列并在工作表2中搜索匹配,如果找到任何匹配的列,则替换该值。查找匹配的复合数据并使用宏替换

Workshhet1

Worksheet2

目前我已经固定了找到匹配的一列。这里是我的代码

Sub FindMatch() 
     Dim x As String 
     Dim found As Boolean 
     ' Select first line of data. 
     Range("A2").Select 
     ' Set search variable value. 
     x = "A" 
     ' Set Boolean variable "found" to false. 
     found = False 
     ' Set Do loop to stop at empty cell. 
     Do Until IsEmpty(ActiveCell) 
     ' Check active cell for search value. 
     If ActiveCell.value = x Then 
      found = True 
      Exit Do 
     End If 
     ' Step down 1 row from present location. 
     ActiveCell.Offset(1, 0).Select 
     Loop 
    ' Check for found. 
     If found = True Then 
     MsgBox "Value found in cell " & ActiveCell.Address 
     Else 
     MsgBox "Value not found" 
     End If 
    End Sub 

我想在工作表2 我搜索的SO搜索栏“A”和“d”,在工作表1对列“A” &“B”,但没有看到任何职务类似于我的要求。谁能帮我吗。

谢谢!

+1

所以..你试图比较哪个值与哪个值?你尝试了什么,它没有工作?也许展示一个期望结果的例子。 –

+0

使用Match,它是一个公式,然后在它周围包装一个IsError,让IsError与IsError不匹配。 – Lowpar

+0

对不起,我没有太多的信誉上传超过2个图像链接。我用一些代码片段更新了我的问题。 @Miguel_Ryu – Joe

回答

0

这应该做到这一点。

Sub FindMatch() 

    Dim WS_one As Worksheet 
    Dim WS_two As Worksheet 

    Set WS_one = Worksheets("Sheet1") 
    Set WS_two = Worksheets("Sheet2") 
    ' Set worksheets to be used. 

    Set lastCell = WS_one.Range("A:A").Find _ 
     ("*", after:=Cells(1, 1), SearchDirection:=xlPrevious) 
    'set lastCell as last cell with any value 
    For x = 1 To lastCell.Row 
    'Set For loop to run until last cell with value. 

     With WS_one 
      'with sheet one only 
      .Select 
      Col_ValueA = .Cells(x, 1).Value 
      Col_ValueD = .Cells(x, 4).Value 
      'define value to search for 
     End With 

     With WS_two 
      'with sheet two only 
      .Select 

      Set findvalue = .Range("A:A").Find _ 
       (Col_ValueA, after:=Cells(1, 1)) 
      'find 1st value equal to column A value 

      Do 
      'Do while no match found, and still values to check 

       If Not findvalue Is Nothing Then 
        'if value exist then check for match 

        If .Cells(findvalue.Row, 2).Value = Col_ValueD Then 
         'check double match 
         ReplaceValue = .Cells(findvalue.Row, 3).Value 
         Exit Do 
        Else 
         'if second value doesn't match find new Column A value 
         temp = findvalue.Row 
         'security check 

         Set findvalue = .Range("A:A").Find _ 
          (Col_ValueA, after:=Cells(temp, 1)) 
         'find new row 

         If temp = findvalue.Row Then 
         'if row doesnt change exit do 
          Exit Do 
         End If 
        End If 
       Else 
        'if no column A match found exit do 
        Exit Do 
       End If 

      Loop 
     End With 

     If Not ReplaceValue = "" Then 
      ' replacement exists paste in column E and reset replacement value 
      WS_one.Cells(x, 5).Value = ReplaceValue 
      ReplaceValue = "" 
     End If 
    Next 
    WS_one.Select 

End Sub 

本质上,代码将表单A的A列与表2匹配,如果该值匹配,则检查第二个值。顺便说一下,您可能会注意到我发布的代码没有选择范围和活动单元了,尽量不要经常使用它们,因为它们不是非常安全,并且会使代码变得更慢。

+0

你是一名现场救星!非常感谢 !! – Joe