【发布时间】:2017-03-15 05:00:02
【问题描述】:
你好,
感谢您最近对我的支持。我正在使用 excel 表(图片附在此处),在这里我有大约 60k 行在 A 列中重复相同的日期。实际上我需要做的是通过用户表单选择开始日期和结束日期(可以在图像中看到) .何时单击“确定”按钮,应删除日期超出给定日期范围的其余行。 但我的代码并没有完全按照我的意愿工作并删除了该范围内的一些行。我承认可能有我的错误,但经过这么多努力我还是找不到。而且组合框中的日期也重复了很多次并且没有排序。请仔细阅读下面的代码-
Private Sub CancelButton_Click()
Unload UserForm1
End Sub
Private Sub ComboBox1_Change()
ComboBox1.Value = Format(ComboBox1.Value, "dd-mmm-yyyy")
End Sub
Private Sub ComboBox2_Change()
ComboBox2.Value = Format(ComboBox2.Value, "dd-mmm-yyyy")
End Sub
Private Sub okButton_Click()
Dim i As Double, dt1 As String, dtt1 As String
Dim dt2 As String, dtt2 As String
Dim ws As Worksheet, lRow As Long
Set ws = ActiveWorkbook.Sheets(1)
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
dt1 = ComboBox1.Value
dtt1 = CDate(dt1)
dt2 = ComboBox2.Value
dtt2 = CDate(dt2)
Application.ScreenUpdating = False
For i = 2 To lRow
If Range("A" & i).Value >= dtt1 And Range("A" & i).Value <= dtt2 Then
Rows(i).Select
Selection.Delete
i = i - 1
End If
Next
Application.ScreenUpdating = True
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
Set ws = ActiveWorkbook.Sheets(1)
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
ComboBox1.RowSource = "A2:A" & lRow
ComboBox2.RowSource = "A2:A" & lRow
End Sub
非常感谢。
【问题讨论】: