我正在尝试运行我的数据循环来检查许多不同的方面,如果有任何失败,宏将删除该行。除了检查日期外,我的一切都在工作。如果给定单元格中的日期等于最初输入的日期,我想保留行,否则我想删除它。但是,我似乎无法将我的约会与工作相比较。有什么想法吗?
Dim myValue As Variant
myValue = InputBox("Enter current as of date (MM/DD/YY)")
Dim dDate As Date
Dim NumberofRows As Long
Dim x As Long
With Worksheets("Transactions")
NumberofRows = .Cells(.Rows.Count, "C").End(xlUp).Row
For x = 1 To NumberofRows
If (.Range("Q" & x).Value) = myValue.Date Then
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.EntireRow.Select
Selection.Delete
Range("A" & (ActiveCell.Row)).Select
End If
Next x
End With
答案 0 :(得分:2)
试试这个:
Sub Test()
Dim myValue As Variant
myValue = InputBox("Enter current as of date (MM/DD/YY)")
If Not IsDate(myValue) Then Exit Sub
Dim dDate As Date
Dim NumberofRows As Long
Dim x As Long
With Worksheets("Transactions")
NumberofRows = .Cells(.Rows.Count, "C").End(xlUp).Row
For x = NumberofRows To 1 Step -1
If Not .Range("Q" & x).Value = myValue Then
.Range("Q" & x).EntireRow.Delete
End If
Next x
End With
End Sub
答案 1 :(得分:0)
您可以一次删除所有内容,而不是一次删除一个。这也会快一些,你不必倒退。
只需将要删除的所有行添加到范围中,然后删除整个范围。
./node_modules/react-notification-alert/index.js:4
答案 2 :(得分:0)
这是一个使用Date变量进行比较的版本,并检查是否单击了Cancel。这样你就知道你在比较苹果和苹果。
Sub Test()
Dim myValue As Variant
Dim dtValue As Date
Dim dtSheet As Date
myValue = InputBox("Enter current as of date (MM/DD/YY)", "Enter Date", Date)
' user cancelled
If myValue = "" Then Exit Sub
If Not IsDate(myValue) Then
MsgBox "That is not a date.", vbCritical
Exit Sub
End If
' user entered a valid date, so continue
dtValue = CDate(myValue)
Dim NumberofRows As Long
Dim x As Long
With Worksheets("Transactions")
NumberofRows = .Cells(.Rows.Count, "C").End(xlUp).Row
For x = NumberofRows To 1 Step -1
If IsDate(.Range("Q" & x).Value) Then
dtSheet = CDate(.Range("Q" & x).Value)
If Not dtSheet = dtValue Then
.Range("Q" & x).EntireRow.Delete
End If
Else
MsgBox "'" & .Range("Q" & x).Value & "' is not a date.", vbCritical
End If
Next x
End With
End Sub