将当前活动行从一个工作表复制到另一个工作表

时间:2014-02-27 21:51:38

标签: excel vba excel-vba worksheet

我有一个关于将当前活动行从一个工作表复制到另一个工作表的问题。假设我在Sheet1中有以下列名称,地址,城市,电话,邮政编码,我在表2中有类似的列。 假设我现在在第100行或500或1000的表1的名称栏中说。我只想将该行复制到表2并填充表2中的列。到目前为止,我有这段代码。请让我知道如何继续。

Private Sub CommandButton1_Click()
Dim CustomerName As String, Customeraddress As String, Customercity As String, Custtel As String, Custzip As String
Worksheets("sheet1").Select
CustomerName = Range("A2")
Customeraddress = Range("B2")
Customercity = Range("C2")
Custtel = Range("D2")
Custzip = Range("E2")
Worksheets("sheet2").Select
Worksheets("Sheet2").Range("B4").Select
If Worksheets("Sheet2").Range("B4").Offset(1, 0) <> "" Then
Worksheets("Sheet2").Range("B4").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = CustomerName
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Customeraddress
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Customercity
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Custtel
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Custzip
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("C4").Select
End Sub

1 个答案:

答案 0 :(得分:3)

  

命令按钮位于Sheet1

试试这段代码:

Private Sub CommandButton1_Click()
    Dim lastrow As Long

    With ThisWorkbook.Worksheets("Sheet2")
        lastrow = Application.Max(4, .Cells(.Rows.Count, "B").End(xlUp).Row + 1)
        .Range("B" & lastrow).Resize(, 5).Value = _
            Range("A" & ActiveCell.Row).Resize(, 5).Value
    End With
End Sub