【问题标题】:Macro to read cells in one sheet and then search for match in another sheet and add date to column宏读取一张表中的单元格,然后在另一张表中搜索匹配并将日期添加到列
【发布时间】:2013-07-18 16:47:50
【问题描述】:

我正在尝试编写一个 excel 宏,该宏将沿一列文本向下移动,然后在同一工作簿的另一个电子表格中搜索该列中的每个项目。找到该值后,应将今天的日期放在特定列中,例如H 列。我找到了一些代码可以完成第一部分,而另一部分代码可以完成第二部分。我只是没有编码知识来将两者放在一起并使其工作。两段代码如下所示。

Sub From_sheet_make_array()

  Dim myarray As Variant
  Dim cells As Range
  Dim cl



      myarray = Range("a1:a10").Value

      Set cells = Worksheets("Sheet2").Columns(1).cells

      Set cl = cells.cells(1)
      Do
      If IsError(Application.Match(cl.Value, myarray, False)) Then
      Exit Sub
      Else:
            i = i + 1
      'This shows each item in column in messagebox
      'Need to pass this value to other code somehow
            MsgBox (cl.Value)

     End If

设置 cl = cl.Offset(1, 0) 非空时循环(cl.Value) 结束子

Sub Searchlist()

Dim c As Range
With ActiveWorkbook.Sheets("Sheet1")
' This will search for whatever is in ""
' Somehow need to get cl.Value (myarray) in here
    Set c = .Columns("A").Find(What:="text", _
                               LookIn:=xlFormulas, LookAt:=xlPart, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlNext, MatchCase:=True)
             c.Offset(0, 8).Value = Date
End With
Exit Sub

结束子

如果有人可以将它们拼接在一起或指出正确的方向,那真的会帮助我。我可能会走得很远,因为我对此很陌生。感谢您提供的任何帮助。

【问题讨论】:

    标签: arrays excel vba


    【解决方案1】:

    据我了解,您正在检查另一个工作表中是否有今天的条目,如果有,则在当前工作表中添加日期戳。 试试这个:(让我知道它是否有效!)

    Sub DateStampIfFound()
    Dim cell As Range
    Dim temp As Range
    For Each cell In Sheets("worksheet_containing_search_criteria").UsedRange.Columns("Specify_the_column").Cells
        'so you dont search for blanks and skipping header row
        If cell <> "" and cell.row<>1 Then
            Set temp = Sheets("worksheet_where_you_want_the_find_to_happen").Columns("Specify_the_column").Find(What:=cell.Value, _
                                LookIn:=xlFormulas, LookAt:=xlPart, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlNext, MatchCase:=True)
            'if found
            If Not temp Is Nothing Then
                'if the search_criteria is in the same sheet
                cell.Offset(0, number_of_columns_offset_from_cell) = Date
            End If
        End If
    Next
    

    结束子

    【讨论】:

    • 嗨,德里克。感谢您的回复。当我运行宏时,出现运行时错误“13”类型不匹配在线 - 如果单元格 “”。如果我注释掉这一行,那么宏运行正常。但是,它会在包含搜索条件的工作表上加上日期戳(如您在评论中所说)。如何在我希望查找发生的工作表中获取日期戳?
    • 嗨,我的错误是我忘记在 for 行中的“Columns("Specify_the_column")”之后放置“.Cells”。我已经更新了我的代码,请再试一次。
    • 如果你想要找到的单元格旁边的日期戳,而不是 "c​​ell.Offset(0, number_of_columns_offset_from_cell) = Date" 你可以试试 "temp.offset(...)"
    • 非常感谢您对这个 Derek 的帮助。它现在完美运行。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多