EXCEL VBA:将工作表A中的行复制到工作表B的末尾

时间:2015-07-28 14:39:47

标签: excel vba excel-vba

我正在尝试将工作表A中的行复制到工作表B的末尾,而工作表B中已有数据列表。我使用下面显示的代码。问题是每次我尝试运行这个特定代码时,整个程序都会崩溃。

import java.awt.*;
import javax.swing.*;

public class SplitButton extends JPanel {

    // The other half.
    private JButton button2;
    private JButton button3;

    public SplitButton(String left, String right, int width, int height) {
        super();
        //setBackground(Color.GREEN);
        //setBorder(BorderFactory.createLineBorder(Color.BLUE));
        setMinimumSize(new Dimension(width, height));
        FlowLayout flow = new FlowLayout();
        flow.setAlignment(FlowLayout.LEFT);
        flow.setHgap(0);
        flow.setVgap(0);

        setLayout(flow);

        button2 = new JButton(right);
        button2.setMinimumSize(new Dimension(width / 2, height));
        button2.setMaximumSize(new Dimension(width / 2, height));
        button2.setPreferredSize(new Dimension(width / 2, height));
        add(button2);

        button3 = new JButton(right);
        button3.setMinimumSize(new Dimension(width / 2, height));
        button2.setMaximumSize(new Dimension(width / 2, height));
        button2.setPreferredSize(new Dimension(width / 2, height));
        add(button3);
    }
}

1 个答案:

答案 0 :(得分:2)

以下作品。我已经为A表添加了一个lastrow,以便它更具动感。我也犯了错误,你不需要在开始时设置每个循环

Sub test()
Dim i As Integer
Dim j As Integer
Dim Last_row as Integer

i = Sheets("B").Range("A1").End(xlDown).Row + 1
last_row = Sheets("A").Range("A1").End(xlDown).Row

On Error GoTo Err_Execute
For j = 1 To last_row
    Sheets("A").Rows(j).EntireRow.Copy
    Sheets("B").Rows(i).PasteSpecial
    i = i + 1
Next j

Err_Execute:
        If Err.Number = 0 Then MsgBox "All have been copied!" Else _
        MsgBox Err.Description
End Sub

实际上你为什么要循环?下面只选择所有行并在一个操作中复制它们。应该快得多

Sub test()
On Error GoTo Err_Execute
With Sheets("A")
    .Rows("1:" & .Range("A1").End(xlDown).Row).EntireRow.Copy
End With
With Sheets("B")
    .Rows(.Range("A1").End(xlDown).Row + 1).PasteSpecial
End With

Err_Execute:
        If Err.Number = 0 Then MsgBox "All have been copied!" Else _
        MsgBox Err.Description
End Sub