如何将单元格复制到最后一行并粘贴到另一张表格?

时间:2012-05-17 19:21:27

标签: excel-vba vba excel

我有一个ActiveSheet脚本,我在其中将原始数据移动到行Q:V。我有一个运行的VBA脚本,并显示最后一行的位置,在这种情况下,最后一行是77。

lastrow = .Cells(.Rows.Count, "Q").End(xlUp).Row

我想把它从Q到V的最后一行,复制并粘贴到表1 ...

我猜它看起来会像这样,但我想先在这里验证一下......因为我去的正常网站由于某种原因需要维护。

Sub test()
         Dim wsPOD As Worksheet
    Dim wsPOT As Worksheet
    Dim wsPOA As Worksheet
    Dim cel As Range
    Dim lastrow As Long, i As Long, Er As Long

    Set wsPOD = Sheets("PO Data")
    Set wsPOT = Sheets("PO Tracking")
    Set wsPOA = Sheets("PO Archive")

        With ActiveSheet
            .AutoFilterMode = False
            Intersect(.UsedRange, .Columns("A")).Cut .Range("Q1")
            Intersect(.UsedRange, .Columns("D")).Cut .Range("R1")
            Intersect(.UsedRange, .Columns("C")).Cut .Range("S1")
            Intersect(.UsedRange, .Columns("B")).Cut .Range("T1")
            Intersect(.UsedRange, .Columns("G")).Cut .Range("U1")
            Intersect(.UsedRange, .Columns("F")).Cut .Range("V1")
            lastrow = .Cells(.Rows.Count, "N").End(xlUp).Row
            Intersect (.UsedRange.Range("Q:V" & lastrow).Copy)
            Intersect (wsPOT.Range("B3:H" & lastrow).PasteSpecialxlPasteFormats)
         End With
End Sub

这显然不起作用,如果有人可以帮助我,我们将不胜感激。

2 个答案:

答案 0 :(得分:2)

这是你正在尝试的>

 With ActiveSheet
    .AutoFilterMode = False
    '
    '~~> Rest of the code
    '
    lastRow = .Range("N" & Rows.Count).End(xlUp).Row
    .Range("Q1:V" & lastRow).Copy
    wsPOT.Range("B3").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
 End With

xlPasteFormats只会粘贴格式,而不会粘贴值。如果您想粘贴值,请将xlPasteFormats更改为xlPasteValues

答案 1 :(得分:0)

Option Explicit
Sub copylocation()
  Dim EC As Long
  Dim X As Long
  Dim Y As Long
  X = Range("B1").End(xlUp).Offset(1, 0).Row
  EC = Range("b1").End(xlToLeft).Offset(0, X).Column
  Windows("Book2").Activate
  Range("b1:AB" & EC).Select
  Selection.Copy
  Windows("Book1").Activate
  Range("b1").Select
  ActiveSheet.Paste
End Sub