Excel VBA删除日期行
我在D列中有一列日期,格式为Excel VBA删除日期行,vba,excel,delete-row,Vba,Excel,Delete Row,我在D列中有一列日期,格式为mm dd yyyy格式。下面是我试图用来删除整行数据的代码,如果列D中的活动单元格为空白、今天的日期或早于8天(即今天是3/13/14,因此它将删除空白条目、今天的日期和早于3/5/14的任何内容) Dim lastrow尽可能长 lastrow=范围(“A65536”)。结束(xlUp)。行 范围(“D”和最后一行)。选择 做 如果(ActiveCell=”“或ActiveCell=Format(现在是“mm/dd/yyyy”)或ActiveCell
mm dd yyyy
格式。下面是我试图用来删除整行数据的代码,如果列D中的活动单元格为空白、今天的日期或早于8天(即今天是3/13/14,因此它将删除空白条目、今天的日期和早于3/5/14的任何内容)
Dim lastrow尽可能长
lastrow=范围(“A65536”)。结束(xlUp)。行
范围(“D”和最后一行)。选择
做
如果(ActiveCell=”“或ActiveCell=Format(现在是“mm/dd/yyyy”)或ActiveCell
如果我使用“”符号,那么它不会删除日期在2月等的行。有人能建议一种可行的方法吗,或者为什么我的方法不行吗?我只是想了想,但是当你在Excel中使用
格式
关键字时,它可能会将日期转换为文本值,所以你不能对它执行比较操作
请尝试以下方法:
If (ActiveCell = "" Or (ActiveCell = Format(Now, "mm/dd/yyyy")) Or (Cdate(ActiveCell) < (Now -8))) _
If(ActiveCell=”“或(ActiveCell=Format(现在是“mm/dd/yyyy”))或(Cdate(ActiveCell)<(现在是-8)))_
实际上,不是将NOW()-8
更改为文本,而是将Activecell
转换为可以用于比较的日期
再说一次,我没有用VBA来做这件事,但我猜它应该能做到
祝你好运 尝试使用日期差异:
If not isempty(activecell)
If DateDiff("d", Now(), ActiveCell.Value) < -8 then
'do your stuff
endif
endif
如果不是空的(activecell)
如果DateDiff(“d”,Now(),ActiveCell.Value)<-8,则
“做你的事
恩迪夫
恩迪夫
将以下代码粘贴到模块中:
Sub ScrubData()
Dim i As Long
Dim numRowsWithVal As Long
Dim myActiveCell As Range
Dim todaysDate As Date
Dim cutoffDate As Date
'Use a custom function to delete all blank rows in column specified
Call DeleteAllBlankRowsInColumn("D")
'Use VBA's Date() function to get current date (i.e. 3/13/14)
todaysDate = Date
'Set the cutoff date to anything older than 8 days
cutoffDate = todaysDate - 8
'***** Loop through all rows and clear values if rows are equal to today's date or older than 8 days ******
'Count the number of rows with values (subtract one because sheet has headers)
numRowsWithVal = (Range("D" & Rows.Count).End(xlUp).Row) - 1
'Start at Range("D2")
Set myActiveCell = ActiveSheet.Range("D2")
For i = 0 To numRowsWithVal - 1
Select Case True
'If value of cell is today's date OR older than 8 days clear the values
Case myActiveCell.Offset(i, 0).Value = todaysDate, myActiveCell.Offset(i, 0).Value <= cutoffDate
myActiveCell.Offset(i, 0).ClearContents
'Value is valid, do nothing
Case Else
End Select
Next
'***********************************************************************************************************
'Now that values are cleared, delete all blank rows again
Call DeleteAllBlankRowsInColumn("D")
End Sub
Public Function DeleteAllBlankRowsInColumn(ByVal columnLetter As String)
'Delete all blank rows in column specified (suppress errors just in case there aren't any blank cells)
On Error Resume Next
Columns(columnLetter).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Set error handling back to normal
On Error GoTo 0
End Function
subscrubdata()
我想我会坚持多久
与VAL一样长的暗数值
暗淡的myActiveCell作为范围
Dim今天作为日期
将截止日期变为日期
'使用自定义函数删除指定列中的所有空行
调用DeleteAllBlankRowsInColumn(“D”)
'使用VBA的Date()函数获取当前日期(即3/13/14)
今天=日期
'将截止日期设置为早于8天的任何日期
截止日期=今天-8
'****循环所有行,如果行等于今天的日期或早于8天,则清除值******
'使用值计算行数(由于工作表有页眉,因此减去一行)
numRowsWithVal=(范围(“D”和Rows.Count).End(xlUp.Row)-1
'从范围开始(“D2”)
设置myActiveCell=ActiveSheet.Range(“D2”)
对于i=0到numRowsWithVal-1
选择Case True
'如果单元格的值是今天的日期或早于8天,请清除这些值
Case myActiveCell.Offset(i,0).Value=todaysDate,myActiveCell.Offset(i,0).Value
Sub ScrubData()
Dim i As Long
Dim numRowsWithVal As Long
Dim myActiveCell As Range
Dim todaysDate As Date
Dim cutoffDate As Date
'Use a custom function to delete all blank rows in column specified
Call DeleteAllBlankRowsInColumn("D")
'Use VBA's Date() function to get current date (i.e. 3/13/14)
todaysDate = Date
'Set the cutoff date to anything older than 8 days
cutoffDate = todaysDate - 8
'***** Loop through all rows and clear values if rows are equal to today's date or older than 8 days ******
'Count the number of rows with values (subtract one because sheet has headers)
numRowsWithVal = (Range("D" & Rows.Count).End(xlUp).Row) - 1
'Start at Range("D2")
Set myActiveCell = ActiveSheet.Range("D2")
For i = 0 To numRowsWithVal - 1
Select Case True
'If value of cell is today's date OR older than 8 days clear the values
Case myActiveCell.Offset(i, 0).Value = todaysDate, myActiveCell.Offset(i, 0).Value <= cutoffDate
myActiveCell.Offset(i, 0).ClearContents
'Value is valid, do nothing
Case Else
End Select
Next
'***********************************************************************************************************
'Now that values are cleared, delete all blank rows again
Call DeleteAllBlankRowsInColumn("D")
End Sub
Public Function DeleteAllBlankRowsInColumn(ByVal columnLetter As String)
'Delete all blank rows in column specified (suppress errors just in case there aren't any blank cells)
On Error Resume Next
Columns(columnLetter).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Set error handling back to normal
On Error GoTo 0
End Function