Excel VBA-将特定列从一个工作表复制到另一工作表

时间:2018-06-21 05:27:55

标签: excel vba excel-vba

这是我拥有的代码,但似乎有些不正确, 在第二个工作表中似乎仅弹出第一行数据。 有人可以帮忙吗?在此先感谢!

Sub copycolumns()
    Dim lastrow As Long, erow As Long
    lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastrow
        Sheet1.Cells(i, 3).Copy
        erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Sheet1.Paste Destination:=Worksheets(“Sheet2”).Cells(erow, 1)

        Sheet1.Cells(i, 4).Copy
        Sheet1.Paste Destination:=Worksheets(“Sheet2”).Cells(erow, 2)

        Sheet1.Cells(i, 6).Copy
        Sheet1.Paste Destination:=Worksheets(“Sheet2”).Cells(erow, 3)
    Next i

    Application.CutCopyMode = False
    Sheet2.Columns().AutoFit
End Sub

嗨!编辑我以前的代码。

我发现代码比以前的代码更有效

Sub CopyPastingColumns()

昏昏欲睡

Worksheets(“ Sheet1”)。选择

erow = ActiveSheet.Cells(1,1).CurrentRegion.Rows.Count + 1

    Worksheets("Sheet1").Select
    Worksheets("Sheet1").Range("D6").Select

    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy Sheets("Sheet2").Cells(erow, 1)

erow = ActiveSheet.Cells(1,1).CurrentRegion.Rows.Count + 1

    Worksheets("Sheet1").Select
    Worksheets("Sheet1").Range("I6").Select

    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy Sheets("Sheet2").Cells(erow, 2)

结束字幕

如何串联工作表1中的两行并将其粘贴到工作表2中的任何列中?

例如,两列都是数字,

在Sheet1中, A列为123456,B列为1

我希望在Sheet2列C上输出为1234561

请帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

Sub copycolumns()
    Dim lastrow As Long, erow As Long
    lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1

    For i = 2 To lastrow
        Sheet1.Cells(i, 3).Resize(1, 2).Copy Sheet2.Cells(erow, 1)
        Sheet1.Cells(i, 6).Copy Sheet2.Cells(erow, 3)
        'Edit: added line below
        Sheet2.Cells(erow, 5).Value = Sheet1.Cells(i, 3).Value & ", " & _
                                      Sheet1.Cells(i, 4).Value
        erow = erow + 1
    Next i

    Application.CutCopyMode = False
    Sheet2.Columns().AutoFit
End Sub