复制并粘贴多个单元格

时间:2014-07-12 23:08:06

标签: excel vba loops cells

我正在尝试将一个工作簿中的多个单元格复制并粘贴到另一个工作簿中。我试图将源单元格放入一行并与其他工作簿循环,以复制和粘贴前一个(因此为lastrow语句)下的单元格。

我的代码似乎没有工作,效率非常低!正如您将注意到,单元格不在一个可以轻松复制的整洁范围内。

See below:
    ...        
        lastrow2 = shtBatchwbkBGAsumm.Range("B65536").End(xlUp).Row + 1
        shtWBc2.Unprotect Password:="QC"

        shtWBc2.Range("C4").Value = shtBatchwbkBGAsumm.Range("B" & lastrow2).Value
        shtWBc2.Range("C5").Value = shtBatchwbkBGAsumm.Range("C" & lastrow2).Value
        shtWBc2.Range("D10").Value = shtBatchwbkBGAsumm.Range("D" & lastrow2).Value
        shtWBc2.Range("G10").Value = shtBatchwbkBGAsumm.Range("E" & lastrow2).Value
        shtWBc2.Range("J10").Value = shtBatchwbkBGAsumm.Range("F" & lastrow2).Value
        shtWBc2.Range("M10").Value = shtBatchwbkBGAsumm.Range("G" & lastrow2).Value
        shtWBc2.Range("P10").Value = shtBatchwbkBGAsumm.Range("H" & lastrow2).Value
        shtWBc2.Range("S10").Value = shtBatchwbkBGAsumm.Range("I" & lastrow2).Value
        shtWBc2.Range("C14").Value = shtBatchwbkBGAsumm.Range("J" & lastrow2).Value
        shtWBc2.Range("C15").Value = shtBatchwbkBGAsumm.Range("K" & lastrow2).Value
        shtWBc2.Range("D20").Value = shtBatchwbkBGAsumm.Range("L" & lastrow2).Value
        shtWBc2.Range("G20").Value = shtBatchwbkBGAsumm.Range("M" & lastrow2).Value
        shtWBc2.Range("J20").Value = shtBatchwbkBGAsumm.Range("N" & lastrow2).Value
        shtWBc2.Range("M20").Value = shtBatchwbkBGAsumm.Range("O" & lastrow2).Value
        shtWBc2.Range("P20").Value = shtBatchwbkBGAsumm.Range("P" & lastrow2).Value
        shtWBc2.Range("S20").Value = shtBatchwbkBGAsumm.Range("Q" & lastrow2).Value
        shtWBc2.Range("C24").Value = shtBatchwbkBGAsumm.Range("R" & lastrow2).Value
        shtWBc2.Range("C25").Value = shtBatchwbkBGAsumm.Range("S" & lastrow2).Value
        shtWBc2.Range("D30").Value = shtBatchwbkBGAsumm.Range("T" & lastrow2).Value
        shtWBc2.Range("G30").Value = shtBatchwbkBGAsumm.Range("U" & lastrow2).Value
        shtWBc2.Range("J30").Value = shtBatchwbkBGAsumm.Range("V" & lastrow2).Value
        shtWBc2.Range("M30").Value = shtBatchwbkBGAsumm.Range("W" & lastrow2).Value
        shtWBc2.Range("P30").Value = shtBatchwbkBGAsumm.Range("X" & lastrow2).Value
        shtWBc2.Range("S30").Value = shtBatchwbkBGAsumm.Range("Y" & lastrow2).Value
        shtWBc2.Range("C34").Value = shtBatchwbkBGAsumm.Range("Z" & lastrow2).Value
        shtWBc2.Range("C35").Value = shtBatchwbkBGAsumm.Range("AA" & lastrow2).Value
        shtWBc2.Range("D40").Value = shtBatchwbkBGAsumm.Range("AB" & lastrow2).Value
        shtWBc2.Range("G40").Value = shtBatchwbkBGAsumm.Range("AC" & lastrow2).Value
        shtWBc2.Range("J40").Value = shtBatchwbkBGAsumm.Range("AD" & lastrow2).Value
        shtWBc2.Range("M40").Value = shtBatchwbkBGAsumm.Range("AE" & lastrow2).Value
        shtWBc2.Range("P40").Value = shtBatchwbkBGAsumm.Range("AF" & lastrow2).Value
        shtWBc2.Range("S40").Value = shtBatchwbkBGAsumm.Range("AG" & lastrow2).Value
        shtWBc2.Range("C54").Value = shtBatchwbkBGAsumm.Range("AH" & lastrow2).Value
        shtWBc2.Range("C55").Value = shtBatchwbkBGAsumm.Range("AI" & lastrow2).Value
        shtWBc2.Range("D60").Value = shtBatchwbkBGAsumm.Range("AJ" & lastrow2).Value
        shtWBc2.Range("G60").Value = shtBatchwbkBGAsumm.Range("AK" & lastrow2).Value
        shtWBc2.Range("J60").Value = shtBatchwbkBGAsumm.Range("AL" & lastrow2).Value
        shtWBc2.Range("M60").Value = shtBatchwbkBGAsumm.Range("AM" & lastrow2).Value
        shtWBc2.Range("P60").Value = shtBatchwbkBGAsumm.Range("AN" & lastrow2).Value
        shtWBc2.Range("S60").Value = shtBatchwbkBGAsumm.Range("AO" & lastrow2).Value
        shtWBc2.Range("V56").Value = shtBatchwbkBGAsumm.Range("AP" & lastrow2).Value
        shtWBc2.Range("V57").Value = shtBatchwbkBGAsumm.Range("AQ" & lastrow2).Value
        shtWBc2.Range("W65").Value = shtBatchwbkBGAsumm.Range("AR" & lastrow2).Value
        shtWBc2.Range("C44").Value = shtBatchwbkBGAsumm.Range("AS" & lastrow2).Value
        shtWBc2.Range("C45").Value = shtBatchwbkBGAsumm.Range("AT" & lastrow2).Value
        shtWBc2.Range("D50").Value = shtBatchwbkBGAsumm.Range("AU" & lastrow2).Value
        shtWBc2.Range("G50").Value = shtBatchwbkBGAsumm.Range("AV" & lastrow2).Value
        shtWBc2.Range("J50").Value = shtBatchwbkBGAsumm.Range("AW" & lastrow2).Value
        shtWBc2.Range("M50").Value = shtBatchwbkBGAsumm.Range("AX" & lastrow2).Value
        shtWBc2.Range("P50").Value = shtBatchwbkBGAsumm.Range("AY" & lastrow2).Value
        shtWBc2.Range("S50").Value = shtBatchwbkBGAsumm.Range("AZ" & lastrow2).Value
        shtWBc2.Range("V46").Value = shtBatchwbkBGAsumm.Range("BA" & lastrow2).Value
        shtWBc2.Range("V47").Value = shtBatchwbkBGAsumm.Range("BB" & lastrow2).Value
        shtWBc2.Range("P65").Value = shtBatchwbkBGAsumm.Range("BC" & lastrow2).Value
        shtWBc2.Range("G65").Value = shtBatchwbkBGAsumm.Range("BD" & lastrow2).Value


        Application.CutCopyMode = False
        WBc.Close SaveChanges:=False

    End If
Next F........

目前,没有任何东西被复制过来需要很长时间才能完成四个循环 - 我将进行多达40个循环。

提前致谢

1 个答案:

答案 0 :(得分:2)

 lastrow2 = shtBatchwbkBGAsumm.Range("B65536").End(xlUp).Row + 1

这将为您提供第一行的行号,其中ColB中的内容,而不是ColB中包含任何内容的最后一行。也许你需要放弃+ 1

编辑反向复制方向:

a) copying *from* shtBatchwbkBGAsumm 
b) copying *to* shtBatchwbkBGAsumm

这是一种替代方法:

Dim arr, i As Long, rw As Range

'create an array of your "destination" ranges
arr = Array("C4", "C5", "G10", "J10", "M10", "P10") 'add the rest here...

'(a) assign the last row with data to the variable "rw"
'Set rw = shtBatchwbkBGAsumm.Range("B65536").End(xlUp).EntireRow

'(b) assign the first empty row to the variable "rw"
Set rw = shtBatchwbkBGAsumm.Range("B65536").End(xlUp).EntireRow.Offset(1,0)

shtWBc2.Unprotect Password:="QC"

'lbound here will be zero, ubound=(# of ranges-1)
For i = LBound(arr) To UBound(arr)

    'shtWBc2.Range(arr(i)).Value = rw.Cells(i + 2).Value 'direction (a)
    rw.Cells(i + 2).Value = shtWBc2.Range(arr(i)).Value 'direction (b)

Next i

Application.CutCopyMode = False
WBc.Close SaveChanges:=False

这是我使用rw来表示shtBatchwbkBGAsumm上的范围(行) - 使语法更清晰。

E.g。比较:

rw.cells(,"B").Value

VS

shtBatchwbkBGAsumm.Range("B" & lastrow2).Value