0

我正在尝试编写一个 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

Set cl = cl.Offset(1, 0) Loop While Not IsEmpty(cl.Value) End Sub

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

结束子

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

4

1 回答 1

2

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

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

结束子

于 2013-07-19T17:03:16.767 回答