0

I have problem in comparing cells with different value but it is working if the value compared is the same. The problem that i am facing is to compare between cell A1:A100 in worksheet1 and cell B3:B1000 in worksheet 2. If the value in worksheet1 and worksheet2 is equal it will be pasted to cell A in worksheet3 which i am able to do. If the value is not equal it will be pasted to cell B in worksheet3 this is the problem that I am facing.This is my code.

Private Sub CommandButton1_Click()

Dim val1, val2 As String

For i = 1 To 100

val1 = Worksheets("Sheet1").Cells(i, 1)

 For j = 3 To 1000
 val2 = Worksheets("Sheet2").Cells(j, 2)

  If (val1 = val2) Then
   Worksheets("Sheet3").Cells(i, 1) = val2

  ElseIf (val1 <> val2) Then
   Worksheets("Sheet3").Cells(i, 2) = val2

  End If

  Next

Next

End Sub

4

1 回答 1

0

您实际上的意思是......“对于工作表 2 中 B 列中的每个值,如果在 A1:A100范围内找到它”......

终于找到你了。看看这是否有帮助。

Sub test()
    Dim val As Variant
    Dim found As Integer, notFound As Integer

    found = 0
    notFound = 0

    For j = 3 To 1000
        val = Worksheets("Sheet2").Cells(j, 2).Value
        If Not IsError(Application.Match(val, Worksheets("Sheet1").Range("A1:A100"), 0)) Then
            'Comment this line if you don't want to paste the values that are the same
            'Worksheets("Sheet3").Range("A1").Offset(found) = val
            found = found + 1
        Else
            Worksheets("Sheet3").Range("B1").Offset(notFound) = val
            notFound = notFound + 1
        End If
    Next
End Sub
于 2013-10-03T03:33:11.217 回答