我有以下代码(由M.Schalk提供-谢谢!),并希望深入研究。特别是:
每次“ sheet2”的E列中的单元格与“ sheet1”的C列中的单元格匹配时,我都希望“ sheet2”的H列中该行的单元格内容(右边的3个单元格) ),如果“ sheet1”中的匹配行替换了S列中的内容。我希望这是有道理的!
一旦粘贴了单元格内容,我希望循环继续进行,下一个匹配项将粘贴到H列单元格中。
我知道我对此的解释不是很好,所以如果有人可以解决这个问题,我会很感激
Sub Test3()
Dim x As String, y As String
Dim found As Boolean
Dim i As Integer, lastRow As Long
lastRow = Worksheets("PLANNER_ONGOING_DISPLAY_SHEET").Cells(Rows.Count, 3).End(xlUp).Row
x = "test 73"
For i = 4 To lastRow
'If you need to compare it to a cell in sheet2 you can also set the value of x here
y = Worksheets("PLANNER_ONGOING_DISPLAY_SHEET").Cells(i, 3).Value2
If y = x Then
' Do action here, example:
MsgBox "Value found in cell " & "C" & i
End If
Next i
End Sub
答案 0 :(得分:0)
您的代码检查一个工作表中的特定值,但是您要求测试两个工作表中的不同值。所以,我不太忙,我真的很想被欣赏。因此,这是一个基本的宏,它将循环通过一个工作表中的一列,并测试另一工作表列中的每个单元格的值是否匹配。如果找到,它将在第一个工作表中偏移3列,并将该值复制到第一个工作表列S当前行。
'Define your variables
Dim ws1 As Worksheet, ws2 As Worksheet, cel As Range, i As Long
'Assign your worksheet variables
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
'First loop through each cell in Sheet2, column E, start at row 2 to account
'for header row) to get the value to find in sheet1, column C.
For Each cel In ws2.Range("E2:E" & ws2.Range("E" & ws2.Rows.Count).End(xlUp).Row)
'Then loop through each cell in Sheet1, Column C. If you get a match, then
'copy the value from Sheet2, Column H, cel.row to Sheet1, Column S, i row.
For i = 2 To ws1.Range("C" & ws1.Rows.Count).End(xlUp).Row
If cel.Value = ws1.Cells(i, 3).Value Then
ws1.Cells(i, 3).Offset(, 16).Value = cel.Offset(, 3).Value
End If
Next i 'loops through every used cell in Column C for all matches
Next cel 'loop to the next cell in Sheets2, Columns E