Import/append 从一列 (newrawdata) 到另一列 (centrallibrary) 的单元格(如果不是预先存在的)- 匹配函数的日期错误
Import/append cell from one column (newrawdata) to another column (centrallibrary) if not preexisting - date error with Match Function
下面的代码使用 match 函数来检查 C 列单元格中存储的一组新值是否存在于 A 列中存储的预先存在的范围内。
如果是这样,则在 msgbox 提示中,将显示与该值对应的行。否则,错误 n/a 将使用 "if iserror" 语句处理,该语句会将有问题的值附加到列 A
中的下一个空行
问题:
这适用于文本和数字,但匹配功能似乎无法正确 return 日期。日期值将附加到 A 列的底部,无论是否预先存在。
可能的错误:
匹配函数中日期的格式或值
要注意:
下面是设置为注释的行,但在未注释时会为日期创建错误 - 用于显示数据在 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
您需要 Set
使用工作表的 MATCH function.
范围和日期查找对原始值执行得更好
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)
下面的代码使用 match 函数来检查 C 列单元格中存储的一组新值是否存在于 A 列中存储的预先存在的范围内。
如果是这样,则在 msgbox 提示中,将显示与该值对应的行。否则,错误 n/a 将使用 "if iserror" 语句处理,该语句会将有问题的值附加到列 A
中的下一个空行问题:
这适用于文本和数字,但匹配功能似乎无法正确 return 日期。日期值将附加到 A 列的底部,无论是否预先存在。
可能的错误:
匹配函数中日期的格式或值
要注意:
下面是设置为注释的行,但在未注释时会为日期创建错误 - 用于显示数据在 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
您需要 Set
使用工作表的 MATCH function.
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)