最后一整行未复制到其他工作表

时间:2019-07-04 12:28:29

标签: excel vba

我尝试将最后一整行复制到另一张纸上,但是失败 使用这种方法,它只将单个单元格复制到所有行中

Dim lrow As Long

With Worksheets("101")
    lrow = .Range("B" & .Rows.Count).End(xlUp).Row
    .Range("B" & lrow - 1, "M" & lrow).Copy
    Worksheets("EOM").Range("B4").PasteSpecial xlPasteAll
End With

使用此代码会给出错误

Dim shRead As Worksheet
Set shRead = ThisWorkbook.Worksheets("101")

Dim lastRow As Long, lastCol As Long
lastRow = shRead.Cells(shRead.Rows.Count, 2).End(xlUp).Row
lastCol = shRead.Cells(lastRow, shRead.Columns.Count).End(xlToLeft).Column

With shRead
    shRead.Range(lastRow, lastCol).Copy_
      Worksheets("EOM").Range(B4, M4)
End With

错误
shRead.Range(lastRow, lastCol).Copy_

1 个答案:

答案 0 :(得分:0)

代替您的代码

With shRead
  shRead.Range(lastRow, lastCol).Copy_
  Worksheets("EOM").Range(B4, M4)
End With

您必须在范围内放置开始和结束单元格

With shRead
    .Range(.Cells(lastRow, 1), .Cells(lastRow, lastCol)).Copy Worksheets("EOM").Range("B4")
End With

对于目的地,单元格引用应使用双引号

编辑

您可以使用类似于以下代码的代码;为了从所有工作表中获取数据,需要一个循环,您可以根据需要修改代码

Dim i As Integer 
i = 4

For Each ws In ActiveWorkbook.Worksheets 
    With ws
        lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
        lastCol = .Cells(lastRow, .Columns.Count).End(xlToLeft).Column

        .Range(.Cells(lastRow, 1), .Cells(lastRow, lastCol)).Copy Worksheets("EOM").Range("B" & i)
         i = i + 1
     End With
 Next