在数据列中查找最后一行,然后粘贴其他数据并重复

我从这里find了下面的代码。

With Sheets("Sheet1") If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastrow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else lastrow = 1 End If End With 

我的名声太低,所以我不能在那里发表评论。 什么或在哪里输出? 我无法绑定代码?

该代码为您提供工作表的Worksheet.UsedRange属性中的任何列的最后一行,并在其中包含一个值; 而不是具有特定列中的值的最后一个单元格。 这两者可能是一回事,但不能保证是一样的。

为了得到具有特定列(例如列B)中的最后一个值的行,那么这将是更合适的。

 With Sheets("Sheet1") If Application.WorksheetFunction.CountA(.Columns(2)) <> 0 Then lastrow = .Cells(rows.Count, "B").End(xlUp).Row Else lastrow = 1 End If End With 

要使用此lastrow在下一个单元格中设置一个值(第一个空白),请添加1并将其用于Range.Cells属性的row_num参数中。

 With Sheets("Sheet1") If Application.WorksheetFunction.CountA(.Columns(2)) <> 0 Then lastrow = .Cells(rows.Count, "B").End(xlUp).Row Else lastrow = 1 End If .Cells(lastrow + 1, "B") = "my new value" End With