Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 根据日期条件删除范围_Vba - Fatal编程技术网

Vba 根据日期条件删除范围

Vba 根据日期条件删除范围,vba,Vba,我已经编写了下面的代码,它查看CD列中的日期,如果任何给定单元格中的日期早于Todays日期(在H列中找到),则此单元格中的内容将被清除。但是,我希望代码能够清除找到TodayDate之前日期的行中多个列(“CD:CT”)的内容 对如何扩大内容清理有何建议 Sub DeleteRange() Dim i As Long Dim numRowsWithVal As Long Dim myActiveCell As Range Dim todaysDate As D

我已经编写了下面的代码,它查看CD列中的日期,如果任何给定单元格中的日期早于Todays日期(在H列中找到),则此单元格中的内容将被清除。但是,我希望代码能够清除找到TodayDate之前日期的行中多个列(“CD:CT”)的内容

对如何扩大内容清理有何建议

Sub DeleteRange()

    Dim i As Long
    Dim numRowsWithVal As Long
    Dim myActiveCell As Range
    Dim todaysDate As Date

    Worksheets("Sheet1").Activate

    todaysDate = (Range("H" & Rows.Count).End(xlUp).Value)

    numRowsWithVal = (Range("CD" & Rows.Count).End(xlUp).Row)

    Set myActiveCell = ActiveSheet.Range("CD50")

    For i = 0 To numRowsWithVal

        Select Case True

            Case myActiveCell.Offset(i, 0).Value <= todaysDate

                myActiveCell.Offset(i, 0).ClearContents

        End Select

    Next
End Sub
子删除范围()
我想我会坚持多久
与VAL一样长的暗数值
暗淡的myActiveCell作为范围
Dim今天作为日期
工作表(“表1”)。激活
todaysDate=(范围(“H”和Rows.Count).End(xlUp.Value)
numRowsWithVal=(范围(“CD”和Rows.Count).End(xlUp.Row)
设置myActiveCell=ActiveSheet.Range(“CD50”)
对于i=0到numRowsWithVal
选择Case True

Case myActiveCell.Offset(i,0).Value您的代码中有一些错误。下面是它的外观(代码中的注释):

“ClearRange是该子例程的更好名称,因为它不应该删除范围,而是将其清除。
亚净距()
将ws设置为Excel.Worksheet
我想我会坚持多久
与VAL一样长的暗数值
暗淡的myActiveCell作为范围
“将今天定为日期
设置ws=工作表(“表1”)
'您不必激活工作表即可对其单元格进行操作。
'工作表(“表1”)。激活
”“没有必要。有一个VBA内置函数返回今天的日期。
'todaysDate=(范围(“H”和Rows.Count).End(xlUp.Value)
numRowsWithVal=ws.Range(“CD”和ws.Rows.Count).End(xlUp).Row
设置myActiveCell=ActiveSheet.Range(“CD50”)
对于i=myActiveCell.Row到numRowsWithVal
'这不是Select Case的正确用法。
'在选择案例中,您可以在许多可用选项之间进行选择。在这里我们可以
“只有两种选择-日期早于或晚于今天。
'在这种情况下,正则If语句是更好的选择。
'选择大小写为True

'Case myActiveCell.Offset(i,0).Value感谢您给出的答案,它在我的代码中运行得非常出色,并且非常清晰!
Intersect(Columns("CD:CT"), myActiveCell.Offset(i, 0).EntireRow).ClearContents
'ClearRange is better name for this subroutine, since it is not supposed to delete ranges but clear them instead.
Sub ClearRange()
    Dim ws As Excel.Worksheet
    Dim i As Long
    Dim numRowsWithVal As Long
    Dim myActiveCell As Range
    'Dim todaysDate As Date


    Set ws = Worksheets("Sheet1")

    'You don't have to activate a worksheet to operate on its cells.
    'Worksheets("Sheet1").Activate


    'No need for that. There is VBA built-in function that returns today date.
    'todaysDate = (Range("H" & Rows.Count).End(xlUp).Value)

    numRowsWithVal = ws.Range("CD" & ws.Rows.Count).End(xlUp).Row
    Set myActiveCell = ActiveSheet.Range("CD50")

    For i = myActiveCell.Row To numRowsWithVal



        'This is not the proper usage of Select Case.
        'In Select Case you select between many available options. Here we can
        'only two options - the date is earlier or later than today.
        'In this case regular If statement is better choice.
'        Select Case True
'            Case myActiveCell.Offset(i, 0).Value <= todaysDate
'                myActiveCell.Offset(i, 0).ClearContents
'        End Select

        If ws.Range("CD" & i).Value <= VBA.Date Then
            Call ws.Range("CD" & i & ":CT" & i).ClearContents
        End If

    Next


End Sub