在userform的文本框中删除从现在到指定日期的数据

时间:2015-11-13 12:17:51

标签: vba delete-row userform

我有几张包含数据的工作表,所有工作表都以工作表名称中的“input”开头,并且在A列中都有一个日期列。我想创建一个允许用户在文本框中插入日期的用户表单。此日期将引用指定工作表中的日期列。当用户单击“Okay”时,宏应该从Now()中删除工作表中的所有行,直到指定的日期。换句话说,它从底部开始并向上删除。这些行最初不是根据日期排序的。这就是我到目前为止所做的:

Sub Rens_date()
Dim lRow As Long
Dim lcol
Dim iCntr As Long
Dim wb As Workbook

Set wb = ThisWorkbook.Worksheets
With wb
    lRow = wb.Range("A" & Rows.Count).End(xlUp).Row
    lcol = wb.Range("A" & Columns.Count).End(xlUp).Column
    Set deleterange = .Range(Rens_inputbox.Value, .Cells(lRow, lcol))
End With

For Each Row In deleterange
    If wb.Range("A").Cells = Me.Rens_inputbox.Value Then _
        deleterange.Delete
    End If
Next
End Sub

代码无效:/

1 个答案:

答案 0 :(得分:0)

将此代码放在您的Okay按钮上,这应该有效。它将迭代所有工作表的整个第一列,其名称以“input_”开头,查找当前日期与您在textbox中输入的日期之间的日期。

Private Sub CommandButton1_Click()

    Dim actCell As Range
    Dim lastDate As Date
    Dim startDate As Date
    Dim currentCellDate As Date
    Dim ws As Worksheet

    lastDate = CDate(TextBox1.Value)
    startDate = Date

    For Each ws In ActiveWorkbook.Worksheets
        If LCase(ws.Name) Like "input_*" Then
            For Each actCell In ws.ListObjects(1).DataBodyRange.Columns(1).Cells
                currentCellDate = CDate(actCell.Value)
                If currentCellDate > startDate And currentCellDate < lastDate Then
                    actCell.EntireRow.Delete
                End If
            Next
        End If
    Next

End Sub