如果不是先前存在的话,从一列(newrawdata)导入/附加单元格到另一列(centrallibrary) – 匹配函数的date错误

下面的代码使用匹配函数来检查存储在列C的单元格中的新的一组值是否存在于存储在列A中的预先存在的范围内。

如果是,则在msgbox提示符下,显示与该值对应的行。 否则,使用“if iserror”语句来处理错误n / a,该语句会将有问题的值附加到列A中的下一个空行

问题:

这适用于文本和数字,但匹配函数似乎不正确返回date。 date值会附加到列A的底部,无论预先存在。

可能的错误:

在匹配函数中格式化或date值

要注意:

下面是一条设置为注释的行,但是在取消注释的date时创build一个错误 – 用于显示列A中的数据

'MsgBox(“Data pre-exists in row”&MatchAns)

很高兴提供例子。

Option Explicit Sub AppendNewRecords() 'example used - Column A, listed with a number of values including numbers, dates and text, 'Column C contains new raw data some matching Column A and some not, 'append new raw data not matching Column A to the end of Column A 'Declarations Dim NeAvRow As Integer Dim NeAvRecAdr As String 'Declarations Dim ImportRange As Long Dim MatchLookup As Variant Dim MatchArray As Variant Dim MatchAns As Variant 'Use the match function to see if record exists within the range For ImportRange = 1 To Worksheets("sheet2").Cells(Rows.Count, "C").End(xlUp).Row MatchLookup = Cells(ImportRange, 3) MatchArray = ActiveSheet.Range("A:A") MsgBox ("LookupValue " & MatchLookup) MatchAns = Application.Match(MatchLookup, MatchArray, 0) 'MsgBox ("Data pre-exists in row " & MatchAns) 'Find the address of the last empty row in a column NeAvRow = Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row NeAvRecAdr = "A" & NeAvRow + 1 'Next Available row for appending If IsError(MatchAns) Then Range(NeAvRecAdr) = MatchLookup End If Next ImportRange End Sub 

您需要使用工作表的MATCH函数 Set范围和date查找,使用原始值更好地执行查找。

 MatchLookup = Cells(ImportRange, 3).VALUE2 '<~~ use the raw date value for the lookup SET MatchArray = ActiveSheet.Range("A:A") '<~~ SET the range MsgBox ("LookupValue " & MatchLookup) MatchAns = Application.Match(MatchLookup, MatchArray, 0)