查找单元格值,然后在右侧输入时间戳3列(Excel表单按钮)

时间:2018-04-14 03:17:39

标签: vba excel-vba excel

我有一个Excel的VBA脚本,它连接表单按钮以将提供的信息输入到单元格中。一切都有效。但是,我想添加一个按钮,该按钮将查看名为“UserID”(me.UserID.value)的txtbox内容,在电子表格中搜索它,然后在第7列和同一行上放置时间戳。 .Cells(iRow, 6).Value = Format(Now(), "hh:mm AMPM")右边进入时间,我需要输入超时时间。

Private Sub CommandButton1_Click()

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("WalkIns")
' Fined Empty row
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

' Make sure slots aren't left blank
If Trim(Me.FName.Value) = "" Then
  Me.FName.SetFocus
  MsgBox "Please enter your first name"
  Exit Sub
End If

If Trim(Me.LName.Value) = "" Then
  Me.LName.SetFocus
  MsgBox "Please enter your last name"
  Exit Sub
End If

If Trim(Me.UName.Value) = "" Then
  Me.UName.SetFocus
  MsgBox "Please enter your Username (Example: XXXXXX)"
  Exit Sub
End If

If Trim(Me.RFVisit.Value) = "" Then
  Me.RFVisit.SetFocus
  MsgBox "Please enter your reason for visiting the Walk-Up Window"
  Exit Sub
End If

' Enter responses into spreadsheet
With ws

  .Cells(iRow, 1).Value = Format(Now(), "mm/dd/yyyy")
  .Cells(iRow, 2).Value = Me.FName.Value
  .Cells(iRow, 3).Value = Me.LName.Value
  .Cells(iRow, 4).Value = Me.UName.Value
  .Cells(iRow, 5).Value = Me.RFVisit.Value
  .Cells(iRow, 6).Value = Format(Now(), "hh:mm AMPM")

End With

Me.FName.Value = ""
Me.LName.Value = ""
Me.UName.Value = ""
Me.RFVisit.Value = ""

Me.FName.SetFocus

End Sub

我可能真的在想这个。

1 个答案:

答案 0 :(得分:1)

你可能在此之后

Private Sub ExitButton_Click()
    Dim cell As Range
    Set cell = Range("D1", Cells(Rows.count, 4).End(xlUp)).Find(What:=Me.UsId.Value, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

    If cell Is Nothing Then
        MsgBox "UserID '" & Me.UsId.Value & "' not found!", vbExclamation
    Else
        cell.Offset(0, 3) = Format(Now(), "hh:mm AMPM")
    End If
End Sub