本来是表Org里LKP列出现#N/F,就把同行的数据insert到表Notes里成为新一行数据。
Sub Add
Dim wsORG As Worksheet
Dim wsNotes As Worksheet
Dim rLKP As Range
Dim i As Integer
Dim lastRow As Integer
Dim finalRow As Integer
Set wsORG = ThisWorkbook.Sheets("Org")
Set wsNotes = ThisWorkbook.Sheets("Notes")
wsORG.Activate
Application.Calculate
Set rLKP = wsORG.Range("E2", Range("E2").End(xlDown))
wsNotes.Activate
If wsNotes.FilterMode Then wsNotes.ShowAllData
lastRow = wsNotes.Range("B2").End(xlDown).Row
For Each c In rLKP.Cells
If c.Value = "#N/F" Then
i = c.Row
wsNotes.Range("B2").End(xlDown).Offset(1, 0) = wsORG.Cells(i, 1).Value
wsNotes.Range("C2").End(xlDown).Offset(1, 0) = wsORG.Cells(i, 2).Value
wsNotes.Range("D2").End(xlDown).Offset(1, 0) = wsORG.Cells(i, 3).Value
Application.Calculate
End If
Next
finalRow = wsNotes.Range("B2").End(xlDown).Row
If lastRow < finalRow Then
wsNotes.Range("A2").End(xlDown).Select
Selection.AutoFill Destination:=Range("A" & lastRow & ":A" & finalRow)
Application.Calculate
Range("B2").End(xlDown).Select
End If
End Sub
报错1是一旦在原表里没有完整输入整行数据只输入一个/两个,其他留空的话,下一行新数据到了目标表格就会发生串行的现象,串到上面的空白行导致数据错乱。报错2是每次点击连接macro的button还会不断出现重复数据。
尝试过在循环里用cell一一对应的方式,但是发现原表格会连接其他query更新数据,但是目标表格是累计的数据。
我想要目标表格只拿过来同一行对应的数据,每次用更新button的时候也不产生重复数据。