根据输入框日期删除行

  • 本文关键字:日期 删除行 vba excel
  • 更新时间 :
  • 英文 :


我需要特定宏的帮助:我有 excel 电子表格,在"K"列中,我既有空白单元格,也有带有日期的单元格。我需要创建一个宏,它将删除日期在输入框日期之前的所有行。但我也需要保持空白。 我尝试了在网上找到的多个示例,但没有任何效果。 请帮忙, 谢谢

您可以尝试此操作,只需将ws设置为您在其中有日期的任何工作表即可

Sub datechecker()
Dim inputBoxText As String
Dim inputBoxDate As Date
Dim lastRow As Integer
Dim row As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
With ws    
lastRow = .Cells(.Rows.Count, "K").End(xlUp).row
inputBoxText = InputBox("Please enter a Date:", "Date Input Box", "Type Here", 10, 10)
If IsDate(inputBoxText) Then
inputBoxDate = CDate(inputBoxText)
Else
MsgBox "Please enter a correct date"
Exit Sub
End If
On Error GoTo Handler
For row = 1 To lastRow
If Len(.Range("K" & row).Value) > 0 Then
If inputBoxDate > CDate(.Range("K" & row).Value) Then
.Rows(row).EntireRow.Delete
row = row - 1
End If
End If
Next
End With
Exit Sub
Handler:
MsgBox "Please make sure cells are formatted to date"
End Sub

以上评论是正确的,下次请提供您到目前为止尝试过的内容,而不仅仅是要求我们完成所有工作。

但是,这是我解决此问题的方法: 1( 在 D2:D24 中,我有 2016 年到 31 年 12 月 2017 日之间的随机日期。假设您要删除日期在 2017 年 3 月 1 日之前的所有行。在 E 列中,我添加了日期值的数值,这意味着 01-03-2017 = 42795。 您可以使用下一个宏:

Sub cleardatesmallerthan()
Dim i As Integer
For i = 1 To 24
If Sheets(1).Range("e" & i).Value <= 42795 Then
Sheets(1).Range("D" & i).Value = ""
End If
Next i
End Sub

最新更新