声明一个日期在 sheet 中的变量,问题
Declaring a variable with a date found in sheet, issue
目前我正在尝试将一个变量声明为我从 excel 中的工作表中提取的日期,然后使用该声明的日期作为基准,以便我可以删除大于它的所有内容。我认为我的代码对于这项任务来说是可靠的,但我一直遇到的问题是,当我查看我的日期被声明为而不是 1/12/2015 时,它应该出现在 12:00:00am 并且它取代了我从中提取日期的单元格的内容。到目前为止,这是我的代码...
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 不会被删除。
您声明
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/time 0
。 Cells([+14,u)
中的任何日期值很可能大于零,因此该行将被删除。
根据下面的评论,试试这个更新后的代码:
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
目前我正在尝试将一个变量声明为我从 excel 中的工作表中提取的日期,然后使用该声明的日期作为基准,以便我可以删除大于它的所有内容。我认为我的代码对于这项任务来说是可靠的,但我一直遇到的问题是,当我查看我的日期被声明为而不是 1/12/2015 时,它应该出现在 12:00:00am 并且它取代了我从中提取日期的单元格的内容。到目前为止,这是我的代码...
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 不会被删除。
您声明
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/time 0
。 Cells([+14,u)
中的任何日期值很可能大于零,因此该行将被删除。
根据下面的评论,试试这个更新后的代码:
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