使用rangefind
我有三张床单,床单S,床单P和床单数据。
我首先将图纸S的一栏复制到图纸数据。 然后在表单Data的E列中查找ID。 ID在数据表栏E中与P表A栏A相匹配,然后复制相应的ID。
这里的问题是Sheet数据包含214行,而P表包含1110.在比较ID时,行870和871有两个不同的ID,虽然它们是相同的,但它们不被复制。
有人能指导可能的原因吗?
Sub lookup() Dim lLastrow, totalrows As Long Dim rng As Range Dim i As Long 'Copy lookup values from S to Data With Sheets("S") lLastrow = .Cells(.Rows.count, 1).End(xlUp).Row .Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5") .Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5") End With totalrows = Sheets("P").Cells(Sheets("P").Rows.count, "A").End(xlUp).Row For i = 5 To lLastrow 'Search for the value on P_APQP With Sheets("P") Set rng = .Columns(1).Find(Sheets("Data").Cells(i, 5).Value & "*", lookat:=xlWhole) End With 'If it is found put its value on the destination sheet If Not rng Is Nothing Then With Sheets("Data") .Cells(i, 6).Value = rng.Value .Cells(i, 1).Value = rng.Offset(0, 1).Value .Cells(i, 2).Value = rng.Offset(0, 2).Value .Cells(i, 3).Value = rng.Offset(0, 3).Value .Cells(i, 4).Value = rng.Offset(0, 9).Value .Cells(i, 9).Value = rng.Offset(0, 10).Value .Cells(i, 13).Value = rng.Offset(0, 6).Value .Cells(i, 14).Value = rng.Offset(0, 5).Value .Cells(i, 15).Value = rng.Offset(0, 4).Value .Cells(i, 16).Value = rng.Offset(0, 8).Value End With End If Next i End Sub
我会发布整个代码。 我还对你的第一行声明作了调整 – 就像你所做的那样,只有totalrows被声明为Long。 你必须拼出每一个我害怕。
Sub lookup() Dim lLastrow As Long, totalrows As Long Dim rng As Range Dim i As Long With Sheets("S") lLastrow = .Cells(.Rows.Count, 1).End(xlUp).Row .Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5") .Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5") End With totalrows = Sheets("P").Cells(Sheets("P").Rows.Count, "A").End(xlUp).Row For i = 5 To lLastrow 'Search for the value on P_APQP With Sheets("P") 'amended below Set rng = .Columns(1).Find(Trim(Sheets("Data").Cells(i, 5).Value) & "*", lookat:=xlWhole) End With 'If it is found put its value on the destination sheet If Not rng Is Nothing Then With Sheets("Data") .Cells(i, 6).Value = rng.Value .Cells(i, 1).Resize(, 3).Value = rng.Offset(0, 1).Value .Cells(i, 2).Value = rng.Offset(0, 2).Value .Cells(i, 3).Value = rng.Offset(0, 3).Value .Cells(i, 4).Value = rng.Offset(0, 9).Value .Cells(i, 9).Value = rng.Offset(0, 10).Value .Cells(i, 13).Value = rng.Offset(0, 6).Value .Cells(i, 14).Value = rng.Offset(0, 5).Value .Cells(i, 15).Value = rng.Offset(0, 4).Value .Cells(i, 16).Value = rng.Offset(0, 8).Value End With End If Next i End Sub