用工作表中的date声明variables,问题

目前我正在试图声明一个variables作为我从Excel中的工作表中拉出的date,然后使用该声明的date作为基准,所以我可以删除大于它的一切。 我认为我的代码对于这个任务是可靠的,但是我一直持有的问题是当我查看我的date声明,而不是2015年1月12日,因为它应该在上午12:00:00出现,它取代了我从中抽出date的单元格的内容。 这是我的代码到目前为止…

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 

我认为这里的主要问题在于最后的date正在转变成奇怪的时间。 有任何想法吗?

也参考细胞从50拉,y读

2015年1月12日。

编辑:我更新了我的代码,以反映@freemans评论的build议,当我试图给它分配一个值的最后date的位置。 我倒过来了。

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

  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 

我的问题在这里,如果finaldate宣布为1/12/15,为什么15年1月13日不会被删除。

你声明

 Dim finaldate As Date 

你在这里使用它

 Cells(50, y) = finaldate 

和这里

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

但是你从来没有 finaldate ,所以你填写的date/时间为0Cells([+14,u)任何date值很可能会大于零,因此该行将被删除。

根据下面的评论,尝试这个更新的代码:

 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 = "" '----------------------------Note this line: finaldate = 'something! Today? Now? some user specified date? you don't tell us '---------------------------- Set DS = Sheets("Data Summary") Set ALLCs = Sheets("Asset LLC (Input)") 'ALLCs.Select 'to remove the possibility of future ambiguity, make the change below For y = 1 To 40 If InStr(1, AllCs.Cells(13, y), "Timestamp of Execution") Then 'note, you assign TO the left side of the equal sign (=) FROM the right side 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