0

I Have a table that is set out with these headers:

Task ID | Description | Date completed | Time completed

Let say that the table is set out so that Task ID is in cell A3, Description B3, Date Completed C3, and Time Completed D3. In cell A1 I will input the Task ID to be looked up.

What I would like to happen is that when the macro is run, the Task ID entered into cell A1 is found in the table and then the date and time (at the time of running the Macro) are entered into the corresponding cells in columns C and D.

Thanks!

4

3 回答 3

0

听起来你只需要一堆 VLOOKUPS。在单元格 A1 中输入您的任务 ID。在单元格 A2 中输入 VLOOKUP 作为描述(假设您有 100 个 ID)

=VLOOKUP($A$1,$A$4:$D$100,2,FALSE)

并在 A3 中用于日期等

=VLOOKUP($A$1,$A$4:$D$100,3,FALSE)
于 2013-06-08T20:53:44.987 回答
0

以下两个过程代表一种方法来做您感兴趣的事情。第一个使用该FIND函数在您的表中找到与 cell 的内容匹配的任务 ID A1;每当您输入时,第二个运行第一个过程A1

日期和时间记录代码

您需要将此代码粘贴到工作簿中的标准 VBA 模块中。Visual Basic通过从功能区的选项卡中选择,Developer然后在 VBA 代码编辑器的主菜单上选择 ,可以插入标准代码模块。InsertModule

请注意,该过程假定任务表位于Sheet1工作簿中。如果它在另一个工作表中,您需要将代码中的名称“Sheet1”更改为正确的名称。

  Sub LogTaskCompletion()
      Dim lastRow As Long
      Dim foundCell As Range
      With ThisWorkbook.Sheets("Sheet1")  '<-- change sheet name here
          If Not .Range("A1").Value = "" Then
              lastRow = .Range("A" & Rows.Count).End(xlUp).Row
              'do the search
              Set foundCell = .Range("A2:A" & lastRow).Find(What:=.Range("A1").Value, _
                            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
              If Not foundCell Is Nothing Then
                  'a match! post the date and time for the task
                  With foundCell
                      .Offset(0, 2).Value = Date
                      .Offset(0, 2).NumberFormat = "mm-dd-yyyy"
                      .Offset(0, 3).Value = TimeValue(Now())
                      .Offset(0, 3).NumberFormat = "hh:mm am/pm"
                  End With
              Else
                  'no match!
                  MsgBox "Cannot find task " & .Range("A1").Value
              End If
              .Range("A1").ClearContents
          End If
      End With
  End Sub

宏触发代码

每当在 中创建条目时,此过程将运行前面的宏A1

它需要安装为任务表所在工作表的私有代码。最简单的方法是右键单击工作表的选项卡,选择View Code,然后将代码粘贴到弹出的编辑器窗格中。

  Private Sub Worksheet_Change(ByVal Target As Range)
      If Intersect(Target, Range("A1")) Is Nothing Then 
          Exit Sub
      End If
      Application.EnableEvents = False
      LogTaskCompletion
      Application.EnableEvents = True
  End Sub

安装代码后,将文件保存为启用宏的 ("xlsm") 工作簿。

于 2013-06-09T01:52:31.557 回答
0

可以通过 和 的组合来Worksheet_Change实现Vlookup。请将此代码放在工作表代码部分。

一旦在A1宏中输入值,就会触发,如果在表中找到值,它会使用 Vlookup 获取相应的值(描述)。它还输入当前日期和时间。

Private Sub Worksheet_Change(ByVal Target As Range)

        On Error Resume Next
        Application.EnableEvents = False

        If Not Intersect(Target, Range("A1")) Is Nothing Then


            Dim lastRow As Long, tblRng As Range
            lastRow = Range("A" & Rows.Count).End(xlUp).Row
            If lastRow <= 3 Then lastRow = 3

            Set tblRng = Range("A3:D" & lastRow)
            dt = Application.VLookup(Target, tblRng, 1, 0)

            If Not IsError(dt) Then
                With Target
                    .Offset(0, 1).Value = Application.VLookup(Target, tblRng, 2, 0)
                    .Offset(0, 2).Value = Date
                    .Offset(0, 2).NumberFormat = "mm/dd/yyyy"
                    ' if you want  date from tbl use Application.VLookup(Target, tblRng, 3, 0)
                    .Offset(0, 3).Value = TimeValue(Now())
                    .Offset(0, 3).NumberFormat = "hh:mm am/pm"
                    ' if you want  date from tbl use Application.VLookup(Target, tblRng, 4, 0)
                End With

            Else
                With Target
                    .Offset(0, 1).Value = vbNullString
                    .Offset(0, 2).Value = vbNullString
                    .Offset(0, 3).Value = vbNullString
                End With
            End If
        End If
        Application.EnableEvents = True
    End Sub

在此处输入图像描述

于 2013-06-09T05:24:11.340 回答