【问题标题】:VBA Search value in column in another column, if not found show which, if found copy offset value to offset imputVBA 在另一列中的列中搜索值,如果找不到,则显示哪个,如果找到,将偏移值复制到偏移量输入
【发布时间】:2018-10-11 13:36:28
【问题描述】:

你能帮我制作一个 VBA 脚本,它将搜索 Sheet1 H:H 列中的值单元格(每一行都有数据),如果它在 Sheet 2 H:H 中找到值,它将从工作表 1 复制偏移量 -6 和在工作表 2 中粘贴偏移量 -6。

如果它没有找到任何东西,它会告诉我它没有找到哪些值。

这就是我目前所拥有的,工作但不是最优的,首先我没有得到“未找到”值的信息,如果没有找到,它只会覆盖并复制该项目。

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

感谢您的帮助

【问题讨论】:

  • 老实说,到目前为止看起来还不错。我不确定为什么它会在搜索后覆盖任何内容...
  • 如果您愿意,您可以在 If 语句中添加 Else 以在 oCellNothing 时将 "Not Found" 或其他内容添加到 ws1.Cells(i, 2)

标签: excel vba copy match offset


【解决方案1】:

试试这个:

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

【讨论】:

  • 你是真正的英雄。说真的,这是正确的,非常感谢
  • 您的脚本运行良好。我会再次感谢你。我想知道您是否可以帮助我将其更改为搜索 2 列,并比较这些列,两者都为真,然后它将粘贴评论。原因是有时我在几个地方搜索的“标签”相同,因此脚本会将其粘贴到正确的名称,但最终会出错,因为它位于错误的位置。
  • @JonasKorani 这应该是一个相对容易的改变。在 Do Loop 中,您需要检查第二列中的值。如果您对此感到困惑,请根据您的尝试提出一个新问题(随时提供指向此问题的链接)。
  • 感谢您的反馈,我们已经尝试了一段时间,但无法使其正常工作:p [Link]stackoverflow.com/questions/53117368/… 大师您是我的救星,请帮我最后一次.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多