[英]Compare Four Columns with two column return?
我遇到了一個問題。 我想要完成的是比較四列,如果單元格匹配則返回同一行中的兩個單元格。 舉個例子,我將A&B和D&E的比較與同一行的F&G輸出進行比較。 目的地並不重要,因為我可以改變它。 我所做的只比較了兩列,它們有效,但它還添加了不應用於該特定行的其他單元格。
Sub Add_XY()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("K").Cells
Dim offs As Long: offs = 2 ' <-- Initial offset, will increase after each match
compareValue = cell.Value & "-" & cell.Offset(, 1).Value
ThisWorkbook.Sheets("Data").Range("K6").Value = compareValue
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("P&T Data").UsedRange.Columns("AI").Cells
'For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Value & "-" & compareCell.Offset(, 1).Value = compareValue Then
ThisWorkbook.Sheets("Data").Range("K6").Value = compareCell.Value & "-" & compareCell.Offset(, 1).Value 'test return value
cell.Offset(, offs).Value = compareCell.Offset(, 5).Value
cell.Offset(, offs + 1).Value = compareCell.Offset(, 6).Value
offs = offs + 4 ' <-- now shift the destination column by 4 for next match
Else
End If
Next compareCell
End If
Next cell
End Sub
使用完全按照圖片中所示輸入的數據。
Sub Test()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
compareValue = cell.Value & "-" & cell.Offset(0, 1).Value
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Offset(0, 3).Value & "-" & compareCell.Offset(0, 4).Value = compareValue Then
cell.Offset(0, 8) = cell.Offset(0, 5)
cell.Offset(0, 9) = cell.Offset(0, 6)
Else
End If
Next compareCell
End If
Next cell
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.