我正在尝试编写一个excel宏,它将在一列文本中向下搜索,然后在同一工作簿中的另一个电子表格中搜索列中的每个项目。找到该值后,应将今天的日期放在特定列中,例如我发现了一些代码将执行第一部分,另一部分代码将执行第二部分。我只是没有将编码知识放在一起并使其工作。这两段代码如下所示。
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) 循环而不是IsEmpty(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
End Sub
如果有人可以将它们拼接在一起或指向我正确的方向,那将真正帮助我。我可能会离开,因为我对此很陌生。感谢您提供的任何帮助。
答案 0 :(得分: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
End Sub