【问题标题】:How do I insert an input box that will ask for a date and then add to Rows after that date?如何插入一个要求输入日期的输入框,然后在该日期之后添加到行?
【发布时间】:2019-05-01 16:29:37
【问题描述】:
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 列中的值相加。我意识到范围行不正确,但我不确定如何插入行在我输入的日期之后。

【问题讨论】:

  • 您很可能希望使用Range.Find 方法,但这高度依赖于正确的格式。在电子表格上,日期是否有时间戳或小数点为 0?
  • 另外,您不能命名子 date,因为这是保留字
  • 它们没有时间戳,当我输入 range.find 时,会出现“参数不是可选的”错误。
  • 正在研究解决方案 - 保持稳定

标签: excel vba


【解决方案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

【讨论】:

  • 如何将求和函数添加到数据之后的第一个空行并将其上方的所有内容添加到单元格 D4?
  • 这是一个完全不同的问题。给它一些时间来尝试弄清楚 - 如果你不能,新问题会得到新帖子。如果这回答了您最初帖子中的问题,您应该接受该解决方案:)
  • 这会找到第一个包含我输入的日期的单元格。如何将其修改为自下而上而不是自上而下?
  • 您可以修改Range.Find 方法的选项。 This 链接会告诉你具体方法
  • 如果找不到日期,我如何将它放到哪里,它会让用户重试?
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 2022-11-30
相关资源
最近更新 更多