如何插入要求输入日期的输入框,然后将其添加到该日期之后的行中?

时间:2019-05-01 16:29:37

标签: excel vba

Sub date
    Dim DTE as Date
    MBox = InputBox(“Enter Friday date”)
    If IsDate(MBox) Then
        DTE = CDate(MBox)
        Range(“F” & Rows.Count).End(xlUp).Offset(1).Select
        Selection.Insert Shift:=xlDown
        Selection.Insert Shift:=xlDown
     Else 
         MsgBox(“This isn’t a date. Try again.”)
    End if
End sub

我需要代码在找到用户在输入框中输入的日期后添加两行,然后将D列中的值相加。我意识到范围行不正确,但是我不确定如何插入行在我输入日期之后。

1 个答案:

答案 0 :(得分:1)

几件事

  1. 您不能命名子date,因为这是保留字
  2. 不需要.Selection
  3. 如果使用Application.Inputbox,则可以控制输入类型(在某种程度上

假设您正在Sheet1上工作,并且要与用户输入范围Column F匹配的日期

Sub Date_Finder()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update
Dim xInput As Date, Found As Range

xInput = Application.InputBox("Enter Date [mm/dd/yyyy]", Type:=1)

    If IsDate(xInput) Then
        Set Found = ws.Range("F:F").Find(xInput)

        If Found Is Nothing Then
            MsgBox "Input not found in range"
        Else
            Found.Offset(1).EntireRow.Insert (xlShiftDown)
            Found.Offset(1).EntireRow.Insert (xlShiftDown)                
        End If
    Else
        MsgBox "Invalid Entry. Ending sub" & vbNewLine & "Entry: " & xInput, vbCritical
    End If

End Sub