复制范围n次,其中n在单元格中指定

时间:2013-03-21 19:01:24

标签: excel vba excel-vba

首先,我是VBA的完全新手。 我试图复制A列和B列(动态行)中的范围“n”次,我在单元格“c2”中将n的值指定为同一工作簿中的另一个工作表。

Column A   Column B

DDDDDD      d345g
THFGJJ      th567
JKHNGF      thgf4
UJHG67      uj768
tkm78y      y7865

这是我尝试的但它给了我一个突破错误。

Sub rangecopy()

    Application.ScreenUpdating = False
    Dim rangeini As Long
    Dim i As Integer
    rangeini = Sheets("sheet1").[a10000].End(xlUp).Row+1 //range of data to copy
    n = Sheets("sheet1").range("c2")//number of times to be copied
    For i = 1 To n

        rangeini.Copy
        Sheets("sheet2").range("A2").PasteSpecial//data to be pasted here

    Next i
    Application.ScreenUpdating = True 

End Sub    

我正在修复并尽快需要它。任何帮助都非常感谢。

请写一个简单的代码,以便新手可以理解并从中学习。

2 个答案:

答案 0 :(得分:1)

这只是一个简单的复制/粘贴

Sub rangecopy()

    Dim i As Integer, n As Integer
    Dim intHowmany As Integer


    n = Sheets("sheet1").Range("c2") 'number of times to be copied

    Range("a2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, (Selection.Offset(0, 1))).Select

    Selection.Copy

    intHowmany = Selection.Rows.Count

    ActiveWorkbook.Worksheets(2).Select
    Range("a2").Select


    For i = 1 To n

        ActiveSheet.Paste
        ActiveCell.Offset(intHowmany, 0).Select


    Next i

End Sub

答案 1 :(得分:0)

Sub rangecopy() 

    Application.ScreenUpdating = False 
    Dim rangeini As range  
    Dim i As Integer, n as integer
    Set rangeini = Sheets("sheet1").range("a2").resize(Sheets("sheet1").[a2].End(xlDown).Row - 1, 2) 'range of data to copy 
    n = Sheets("sheet1").range("c2") 'number of times to be copied

    Sheets("sheet2").[a2] = rangeini 'paste the first one in "a2"
    For i = 2 To n

        Sheets("sheet2").[a1].Offset(Sheets("sheet2").[a2].End(xlDown).row) = rangeini  'data to be pasted at the end 

    Next i 
    Application.ScreenUpdating = True

End Sub