发布一个带有在工作表中找到的日期的变量

时间:2015-04-17 13:29:12

标签: excel vba date excel-vba time

目前我正在尝试将变量声明为我从excel中的工作表中提取的日期,然后使用该声明的日期作为基准,以便我可以删除大于它的所有内容。我认为我的代码对于这个任务是可靠的,但我一直有的问题是当我查看我的日期被宣布为而不是2015年1月12日,因为它应该在凌晨12:00:00出现并且它取代了我从中提取日期的单元格的内容。到目前为止,这是我的代码......

Sub Remove_Unecessary_Data_1()

Dim ALLCs As Worksheet
Dim DS As Worksheet
Dim Dailystring As String
Dim Blank As String
Dim finaldate As Date


Dailystring = " - Daily"
Blank = ""

Set DS = Sheets("Data Summary")
Set ALLCs = Sheets("Asset LLC (Input)")



    ALLCs.Select
        For y = 1 To 40
           If InStr(1, Allcs.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, Allcs.Cells(13, u), "Start Date") Then
                For p = 2 To 69584
                If allcs.Cells(p + 14, u) > finaldate Then
                allcs.Cells(p + 14, u).EntireRow.Delete
                End If
                Next
            End If
        Next

我认为这里的主要问题在于最终日期正在转变为奇怪的时间。有什么想法吗?

另外参考细胞从50,y读取

2015年1月12日

编辑:我更新了我的代码,以反映@freemans在尝试为其分配值时对我的最终日期的位置发表评论的建议。我把它颠倒了。

我的新问题是我的代码的第二部分没有准确删除所有必需的日期。例如,我需要删除大约1/12/15的所有日期,我的程序仍然没有这样做,我认为我的代码中没有看到错误

        ALLCs.Select
        For u = 1 To 40
            If InStr(1, Allcs.Cells(13, u), "Start Date") Then
                For p = 2 To 69584
                If allcs.Cells(p + 14, u) > finaldate Then
                allcs.Cells(p + 14, u).EntireRow.Delete
                End If
                Next
            End If
        Next

我的问题在于,如果最终日期被宣布为1/12/15,为什么不删除1/13/15。

1 个答案:

答案 0 :(得分:5)

你宣布

Dim finaldate As Date

你在这里使用

Cells(50, y) = finaldate

在这里

            If Cells(p + 14, u) > finaldate Then
            Cells(p + 14, u).EntireRow.Delete
            End If

但您从未将值分配给 finaldate,因此您需要填写0的日期/时间。 <{1}}中的任何日期值很可能大于零,因此该行将被删除。

根据以下评论,请尝试以下更新代码:

Cells([+14,u)