我正在尝试避免重复行,因此我想检查最后一行的数据是否与新值不同,如果是,则添加新行与新数据。
不知怎的,这不起作用,并且不断添加新值:
Set ws = Worksheets("mySheet")
lastrow = ws.Cells(rows.Count, 1).End(xlUp).Row
If item = ws.Cells(lastrow , 5).Value And color = ws.Cells(lastrow, 8).Value And price = ws.Cells(lastrow, 9).Value Then
' do nothing
Else
' add new rows
newrow = ws.Cells(rows.Count, 1).End(xlUp).Row + 1
ws.Cells(newrow , 5).Value = item
ws.Cells(newrow , 8).Value = color
ws.Cells(newrow , 9).Value = price
End If
显然,我有其他列填充了其他数据,但这些对于双重性并不重要。我的第一列包含DateTime值。
答案 0 :(得分:0)
没有任何数据示例很难说明发生了什么
同时你可能想尝试这个小重构
With Worksheets("mySheet") '<--| reference your relevant worksheet
With .Cells(.Rows.Count, 1).End(xlUp) '<--| reference its column "A" last not empty cell
If Not (item = .Offset(, 4).Value And Color = .Offset(, 7).Value And Price = .Offset(, 8).Value) Then '<-- check if write a new record: do it referencing cells to be compared as offset from referenced one
.Offset(1, 4).Value = item ' <--| reference new record cells as offsetted 1 row doen the referenced one
.Offset(1, 7).Value = Color ' <--| same as above
.Offset(1, 8).Value = Price ' <--| same as above
End If
End With
End With