我有以下代码:
Dim i As Long
Dim J As Integer
Dim LastRow As Long
Dim a As Worksheet
Dim b As Worksheet
Set a = Workbooks("500 PRO").Worksheets(1)
Set b = ActiveSheet
LastRow = a.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
If Not IsError(Application.Match(a.Cells(i, 2).Value, b.Columns(2), 0)) Then
Set FindRow = b.Columns(2).Find(a.Cells(i, 2))
J = FindRow.Row
If IsEmpty(b.Cells(J, 6)) = True Then
b.Cells(J, 6).Value = a.Cells(i, 10).Value
End If
If IsEmpty(b.Cells(J, 7)) = True Then
b.Cells(J, 7).Value = a.Cells(i, 14).Value
End If
End If
Next i
不幸的是,使用应用程序匹配只查看一列中的前几个数字,而不是完整的数字。
我试过这样的方法:
If Not IsError(Application.Find(a.Cells(i, 2).Value, b.Columns(2), 0)) Then
但它也遇到了同样的问题。
代码比较两个工作表a和b中的值。这些是ID号。如果两个ID号匹配,它将检查第一个工作表上的两个值是否为空,如果是,则从第二个工作表中提取缺少的值。
发布于 2017-08-24 15:55:30
我不能确定,因为您没有提供可供使用的示例数据,但要成功使用Range.Find
,请尝试执行以下操作:
Dim foundCell As Range
With b.Columns(2)
For i = 2 To LastRow
If a.Cells(i, 2) <> "" Then
Set foundCell = .Find(what:=a.Cells(i, 2), _
lookat:=xlWhole, _
LookIn:=xlValues, _
MatchCase:=False)
If Not foundCell Is Nothing Then
'...
'check for blanks and copy
'...
End If
End If
Next i
End With
https://stackoverflow.com/questions/45862175
复制