循环复制并粘贴

时间:2015-06-12 09:07:29

标签: vba excel-vba excel

我一直在寻找一种有效的方法将数据从一个电子表格复制到另一个电子表格,并始终在下面粘贴一行。有人帮我使用了这段代码,但遗憾的是它不能用于我需要的列。因此,我需要复制E2中的数据:P2表格和#34;红利"并首先粘贴在C11:N11上,然后明天如果我再次运行应该粘贴在C12:N12并且总是在下面一行...当我运行代码时,它将数据粘贴在C111:N111上,如果我再次运行仍然粘贴在相同的范围内,所以对我不起作用。我很感激你的帮助。谢谢

Sub Copy_range()

    ' edit line below to change where data will be copied from
    Worksheets("Dividends").Range("E2:P2").Copy ' copy the value

    ' select the first cell on the "Draft" sheet
    Worksheets("Draft").Select
    ActiveSheet.Range("C11").Select

    Dim count As Integer
    count = 1

    'skip all used cells
    Do While Not (ActiveCell.value = None)
        ActiveCell.Offset(1, 0).Range("C11").Select
        count = count + 1

    Loop


    Worksheets("Draft").Range("C11" & count & ":N11" & count).PasteSpecial ' paste the value


End Sub

4 个答案:

答案 0 :(得分:0)

使用ActiveCell和Offset通常会导致意外结果并使代码难以阅读。你可以让计数循环完全没有这一切,只需通过从C11开始的C列单元格并寻找空单元格。

可能的方法之一是

    Sub Copy_range
      Dim count As Integer
      count = 11
      Do While Worksheets("Draft").Range("C" & count).Value <> ""
      '<>"" means "is not empty", as long as this happens we go down looking for empty cell
        count = count + 1
      Loop
      'Now count is row with first empty cell outside of top 10 rows in column C

      Worksheets("Dividends").Range("E2:P2").Copy
      Worksheets("Draft").Range("C" & count).PasteSpecial xlPasteValues

    End Sub

答案 1 :(得分:0)

我想说,您很可能只需将Vlookup Formula自动填充到目标区域即可解决此问题。但是下面的代码应该做到这一点。

Option Explicit
Sub moveDividends()

Dim wsF As Worksheet 'From
Dim wsD As Worksheet 'Destination
Dim i As Long
Dim LastRow As Long

Set wsF = ThisWorkbook.Sheets("Sheet1")
Set wsD = ThisWorkbook.Sheets("Sheet2")

    With wsD
        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

    With wsD
        LastRow = LastRow + 1
            wsD.Cells(LastRow, "C").Value = wsF.Cells(2, 5).Value
            wsD.Cells(LastRow, "D").Value = wsF.Cells(2, 6).Value
            wsD.Cells(LastRow, "E").Value = wsF.Cells(2, 7).Value
            wsD.Cells(LastRow, "F").Value = wsF.Cells(2, 8).Value
            wsD.Cells(LastRow, "G").Value = wsF.Cells(2, 9).Value
            wsD.Cells(LastRow, "H").Value = wsF.Cells(2, 10).Value
            wsD.Cells(LastRow, "I").Value = wsF.Cells(2, 11).Value
            wsD.Cells(LastRow, "J").Value = wsF.Cells(2, 12).Value
            wsD.Cells(LastRow, "K").Value = wsF.Cells(2, 13).Value
            wsD.Cells(LastRow, "L").Value = wsF.Cells(2, 14).Value
            wsD.Cells(LastRow, "M").Value = wsF.Cells(2, 15).Value
            wsD.Cells(LastRow, "N").Value = wsF.Cells(2, 16).Value
    End With
End Sub

答案 2 :(得分:-1)

所有方法都是严格的,或者只是使用:

Sub Copy_range()
    Dim lastRow As Long
    ' edit line below to change where data will be copied from
    Worksheets("Dividends").Range("E2:P2").Copy ' copy the value
    ' find the 1th not-used rows
    lastRow = Worksheets("Draft").Cells(1048576, 3).End(xlUp).Row + 1
    lastRow = IIf(lastrows < 11, 11, lastrows) 'optional if is possible that the rows 10, 9, 8,.... are empty
    Worksheets("Draft").Range("C" & lastRow).PasteSpecial xlPasteValues ' paste the value
End Sub

答案 3 :(得分:-2)

使用以下

Sub Copy_range()
    ' edit line below to change where data will be copied from
    Worksheets("Dividends").Range("E2:P2").Copy ' copy the value

    'count cells and add 1 for next row
    last_row = Worksheets("Draft").Range("C" & Worksheets("Draft").Rows.Count).End(xlUp).Row + 1
    If last_row > 1000000 Then last_row = 1

    Worksheets("Draft").Range("C" & last_row ).PasteSpecial 
    ' paste the value only need to ref first cell
End Sub