我在一张纸上有两个单元格 - 我们称之为DATA表格。 我有另一张纸 - 让我们称它为SEARCH表。 我想比较DATA中的两个单元格,我有一个SEARCH信息。
让我们说,如果:( DATA中的单元格值在SEARCH中的X列中)和(SEARCH中Y列中DATA中的单元格值) - 两者都在SEARCH中的同一行 - 然后给我第一个工作表SEARCH中此行的值。
是否可以使用VBA?
感谢
答案 0 :(得分:1)
是的,这是可能的:)你有3种方式:
<强> 1。循环(示例For)
Function FindPair(ByRef CellA As Range, ByRef CellB As Range) As Variant ' <- Change Variant for specific Type of data
'Set default value if not found match
Set FindPair = Nothing
Dim sheetToSearch As Worksheet
Dim lastRow As Long
'Check input
If CellA Is Nothing Or CellB Is Nothing Then
Call MsgBox("Something is wrong!", vbExclamation)
Exit Function
Exit Function
'For easy ro read
Set sheetToSearch = Sheets("Search")
lastRow = (sheetToSearch.UsedRange.Cells(1, 1).Row + sheetToSearch.UsedRange.Rows.Count) 'First row of UsedRange + rows count
'Look up
For r = 0 To lastRow
'Change column here, if you need
If sheetToSearch.Range("A" + r).Value = CellA.Value Then
If sheetToSearch.Range("B" + r).Value = CellB.Value Then
'Return data
FindPair = sheetToSearch.Range("A" + r).Value
Exit For
End If
End If
Next r
'You can compare *.Value = *.Value - for compare binary data
'or *.Text = *.Text - for compare value like as text with formatting
End Function
的 2。查找强>
Application.WorksheetFunction.Find()
请查找文档的工作原理:VBA Find
第3。 VLOOKUP 强>
Application.WorksheetFunction.VLookup()
请查找文档的工作原理:Excel VLookup
答案 1 :(得分:0)
希望你能找到这个......
Sub comp_value_in_diff_cells()
On Error Resume Next
lastrowA = Worksheets("DATA").Range("A" & Rows.Count).End(xlUp).Row
lastrowB = Worksheets("DATA").Range("B" & Rows.Count).End(xlUp).Row
If lastrowA = lastrowB Then
For i = 1 To lastrowA
search1 = Worksheets("DATA").Range("A" & i).Value
search2 = Worksheets("DATA").Range("B" & i).Value
With Worksheets("SEARCH")
For j = 1 To (.UsedRange.Cells(1, 1).Row + .UsedRange.Rows.Count)
a = .Range(j & ":" & j).Find(search1).Row
avalue = .Range(j & ":" & j).Find(search1).Column
b = .Range(j & ":" & j).Find(search2).Row
bvalue = .Range(j & ":" & j).Find(search2).Column
If a = b Then
If avalue < bvalue Then
If Worksheets("DATA").Range("C" & i).Value <> "" Then
Worksheets("DATA").Range("C" & i).Value = Worksheets("DATA").Range("C" & i).Value & "|" & search1
Else
Worksheets("DATA").Range("C" & i).Value = search1
End If
Else
If Worksheets("DATA").Range("C" & i).Value <> "" Then
Worksheets("DATA").Range("C" & i).Value = Worksheets("DATA").Range("C" & i).Value & "|" & search2
Else
Worksheets("DATA").Range("C" & i).Value = search2
End If
End If
End If
Next j
End With
Next i
Else
Msgbox ("DATA Sheet A & B Mismatch")
End If
End Sub