dom*_*176 0 debugging excel vba date excel-vba
目前我正在调试一段代码.目前我的代码按预期工作,它为finaldate变量分配日期,然后在代码中查找删除高于finaldate变量的所有日期.唯一的问题是子程序需要多次运行才能使其生效.例如,当我删除大约一半的日期时,我通过它,再次运行它并且它也一样,我通常F5大约5次以确认它完成.虽然这在调试中很好,但我需要知道每次都能完美运行.
Sub Remove_Unecessary_Data_1()
Dim ALLCs As Worksheet
Dim DS As Worksheet
Dim finaldate As Date
Set DS = Sheets("Data Summary")
Set ALLCs = Sheets("Asset LLC (Input)")
ALLCs.Select
For y = 1 To 40
If InStr(1, Cells(13, y), "Timestamp of Execution") Then
finaldate = ALLCs.Cells(50, y)
End If
Next
ALLCs.Select
For u = 1 To 40
If InStr(1, Cells(13, u), "Start Date") Then
For p = 2 To 69584
If Cells(p + 14, u) > finaldate Then
Cells(p + 14, u).EntireRow.Delete
End If
Next
End If
Next
end sub
Run Code Online (Sandbox Code Playgroud)
编辑:样本数据
细胞(50,y)= 1/12/15 finaldate =细胞(50,Y)
标题为"开始日期"的列包含的日期范围为2015年5月1日至2015年1月30日.
如果正常工作,2015年1月12日之后的所有日期都应该排除整行.
删除行时,您必须从下到上按自己的方式工作,否则最终会跳过行.
例如,你有:
Line 1
>Line 2
Line 3
Line 4
Run Code Online (Sandbox Code Playgroud)
当你的代码删除时,Line 2"Row"3现在变为"Row"2,但你的代码继续进行查看Line 4.您的数据现在如下所示:
Line 1
Line 3
>Line 4
Run Code Online (Sandbox Code Playgroud)
如果你更改了代码的这一部分:
For p = 2 To 69584
If Cells(p + 14, u) > finaldate Then
Cells(p + 14, u).EntireRow.Delete
End If
Next
Run Code Online (Sandbox Code Playgroud)
对此:
For p = 69598 to 16 step - 1
If Cells(p, u) > finaldate Then
Cells(p, u).EntireRow.Delete
End If
Next
Run Code Online (Sandbox Code Playgroud)
一切都应该没问题.
*注意:我将起点和终点调高了14,并+ 14从Cells()参考中删除了.在那里做额外的数学没有意义......
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |