VBA 在另一列的列中搜索值,如果找不到则显示哪个,如果找到则复制偏移值以偏移输入
VBA Search value in column in another column, if not found show which, if found copy offset value to offset imput
你能帮我制作 VBA 脚本,如果它在 [=23] 中找到值,它将搜索列 Sheet1 H:H 中的值单元格(每一行有数据) =] 2 H:H,它将从 sheet 1 复制偏移量 -6 并将偏移量 -6 粘贴到 sheet 2.
如果它没有找到任何东西,它会告诉我它没有找到哪些值。
这就是我所拥有的,工作但不是最佳的,首先我没有得到 "NOT" 找到的值的信息,如果找不到,它只会覆盖并复制该项目。
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim oCell As Range
Dim i As Long
i = 2
Set ws1 = ThisWorkbook.Sheets("Data")
Set ws2 = ThisWorkbook.Sheets("Mellomlagring")
Do While ws1.Cells(i, 1).Value <> ""
Set oCell = ws2.Range("H:H").Find(what:=ws1.Cells(i, 8))
If Not oCell Is Nothing Then ws1.Cells(i, 2) = oCell.Offset(0, -6)
i = i + 1
Loop
Set ws1 = Nothing
Set ws2 = Nothing
感谢您的帮助
试一试:
Sub tgr()
Dim wb As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim rSourceHCol As Range
Dim rSourceHCell As Range
Dim rDestHCol As Range
Dim rFound As Range
Dim sFirst As String
Dim sNotFound As String
Set wb = ActiveWorkbook
Set wsSource = wb.Sheets("Sheet1")
Set wsDest = wb.Sheets("Sheet2")
Set rSourceHCol = wsSource.Range("H2", wsSource.Cells(wsSource.Rows.Count, "H").End(xlUp))
Set rDestHCol = wsDest.Range("H2", wsDest.Cells(wsDest.Rows.Count, "H").End(xlUp))
If rSourceHCol.Row < 2 Then
MsgBox "No values present in column H of source sheet " & wsSource.Name
Exit Sub
ElseIf rDestHCol.Row < 2 Then
MsgBox "No values present in column H of destination sheet " & wsDest.Name
Exit Sub
End If
For Each rSourceHCell In rSourceHCol.Cells
Set rFound = rDestHCol.Find(rSourceHCell.Value, rDestHCol.Cells(rDestHCol.Cells.Count), xlValues, xlWhole)
If rFound Is Nothing Then
sNotFound = sNotFound & Chr(10) & rSourceHCell.Value
Else
sFirst = rFound.Address
Do
rFound.Offset(, -6).Value = rSourceHCell.Offset(, -6).Value
Set rFound = rDestHCol.FindNext(rFound)
Loop While rFound.Address <> sFirst
End If
Next rSourceHCell
If Len(sNotFound) = 0 Then
MsgBox "All values from source data accounted for and updated in destination"
Else
MsgBox "The following values in the source data were not found in destination:" & sNotFound
End If
End Sub
你能帮我制作 VBA 脚本,如果它在 [=23] 中找到值,它将搜索列 Sheet1 H:H 中的值单元格(每一行有数据) =] 2 H:H,它将从 sheet 1 复制偏移量 -6 并将偏移量 -6 粘贴到 sheet 2.
如果它没有找到任何东西,它会告诉我它没有找到哪些值。
这就是我所拥有的,工作但不是最佳的,首先我没有得到 "NOT" 找到的值的信息,如果找不到,它只会覆盖并复制该项目。
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim oCell As Range
Dim i As Long
i = 2
Set ws1 = ThisWorkbook.Sheets("Data")
Set ws2 = ThisWorkbook.Sheets("Mellomlagring")
Do While ws1.Cells(i, 1).Value <> ""
Set oCell = ws2.Range("H:H").Find(what:=ws1.Cells(i, 8))
If Not oCell Is Nothing Then ws1.Cells(i, 2) = oCell.Offset(0, -6)
i = i + 1
Loop
Set ws1 = Nothing
Set ws2 = Nothing
感谢您的帮助
试一试:
Sub tgr()
Dim wb As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim rSourceHCol As Range
Dim rSourceHCell As Range
Dim rDestHCol As Range
Dim rFound As Range
Dim sFirst As String
Dim sNotFound As String
Set wb = ActiveWorkbook
Set wsSource = wb.Sheets("Sheet1")
Set wsDest = wb.Sheets("Sheet2")
Set rSourceHCol = wsSource.Range("H2", wsSource.Cells(wsSource.Rows.Count, "H").End(xlUp))
Set rDestHCol = wsDest.Range("H2", wsDest.Cells(wsDest.Rows.Count, "H").End(xlUp))
If rSourceHCol.Row < 2 Then
MsgBox "No values present in column H of source sheet " & wsSource.Name
Exit Sub
ElseIf rDestHCol.Row < 2 Then
MsgBox "No values present in column H of destination sheet " & wsDest.Name
Exit Sub
End If
For Each rSourceHCell In rSourceHCol.Cells
Set rFound = rDestHCol.Find(rSourceHCell.Value, rDestHCol.Cells(rDestHCol.Cells.Count), xlValues, xlWhole)
If rFound Is Nothing Then
sNotFound = sNotFound & Chr(10) & rSourceHCell.Value
Else
sFirst = rFound.Address
Do
rFound.Offset(, -6).Value = rSourceHCell.Offset(, -6).Value
Set rFound = rDestHCol.FindNext(rFound)
Loop While rFound.Address <> sFirst
End If
Next rSourceHCell
If Len(sNotFound) = 0 Then
MsgBox "All values from source data accounted for and updated in destination"
Else
MsgBox "The following values in the source data were not found in destination:" & sNotFound
End If
End Sub