以下是股票:
Sub CompareWorkbooks()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim dateA As String
Dim dateB As String
Set wbkA = Workbooks.Open(Filename:="C:\Users\xxx\2G.xlsx")
Set varSheetA = wbkA.Worksheets("2G Data")
Set wbkB = Workbooks.Open(Filename:="C:\Users\xxx\2G Data Traffic.xlsx")
Set varSheetB = wbkB.Worksheets("Sheet1")
dateA = wbkA.Worksheets("2G Data").Range("A" & Rows.Count).End(xlUp)
dateB = wbkB.Worksheets("Sheet1").Range("A9")
If dateB = dateA + 1 Then
MsgBox "good"
Else
MsgBox "badd"
Exit Sub
End If
End Sub
答案 0 :(得分:1)
您将dateA和dateB声明为字符串,然后尝试向它们添加一个值并进行比较,但这将无法正常工作。相反,请尝试将它们声明为日期:
Dim dateA As Date
Dim dateB As Date
答案 1 :(得分:0)
首先将您的日期声明为日期。尝试使用DateAdd函数。尝试在字符串或日期中添加1
不会找到您想要的内容。
这是一个例子:
Sub CompareWorkbooks()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim dateA As Date
Dim dateB As Date
Set wbkA = Workbooks.Open(Filename:="C:\Users\xxx\2G.xlsx")
Set varSheetA = wbkA.Worksheets("2G Data")
Set wbkB = Workbooks.Open(Filename:="C:\Users\xxx\2G Data Traffic.xlsx")
Set varSheetB = wbkB.Worksheets("Sheet1")
dateA = wbkA.Worksheets("2G Data").Range("A" & Rows.Count).End(xlUp)
dateB = wbkB.Worksheets("Sheet1").Range("A9")
If dateB = DateAdd("d", 1, dateA) Then
MsgBox "good"
Else
MsgBox "badd"
Exit Sub
End If
End Sub