i在Sheep1中输入4个单元格(A2至A5)的值,现在代码应以VBA从Sheep1中的4个单元中获取值并将其比较并将其比较为Sheet2中的(A1到D1)的单元格应返回每个单元格2(E2至E5)中4个单元格中的结果,表示匹配或不匹配。
我已经很长时间尝试了,但可以使用四次。任何使用循环或可以将代码从30多行更改为几行的东西。
尝试
Sub Demo()
Dim srcSht As Worksheet, destSht As Worksheet
Dim cel As Range, destCell As Range
Dim colIndex As Long
Set srcSht = ThisWorkbook.Sheets("Sheet1") 'source sheet
Set destSht = ThisWorkbook.Sheets("Sheet2") 'destination sheet
Set destCell = destSht.Range("D33")
colIndex = 1
For Each cel In srcSht.Range("A2:A5") 'loop through each cell in range srcSht.Range("A2:A5")
If cel.Value = destSht.Cells(1, colIndex) Then 'check for match
destCell.Offset(colIndex - 1, 0).Value = "Match" 'display result
Else
destCell.Offset(colIndex - 1, 0).Value = "No Match" 'display result
End If
colIndex = colIndex + 1
Next cel
End Sub