简体   繁体   English

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

[英]Excel VBA - Copying specific columns from one worksheet to another worksheet

This is the code that I have, but something seems to be incorrect, Only the first row of data seems to pop up in the second worksheet. 这是我的代码,但是似乎有些不正确,第二个工作表中似乎只弹出第一行数据。 Can somebody help with it? 有人可以帮忙吗? Thanks in advance!! 提前致谢!!

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

Hii!! 嗨! editing my previous code. 编辑我以前的代码。

I found a code more efficient than my previous code, 我发现代码比以前的代码更有效,

Sub CopyPastingColumns() 子CopyPastingColumns()

Dim erow As Long 昏昏欲睡

Worksheets("Sheet1").Select 工作表(“ Sheet1”)。选择

erow = ActiveSheet.Cells(1, 1).CurrentRegion.Rows.Count + 1 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 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)

End Sub 结束子

How to concatenate two rows from sheet 1 and paste it in any column in Sheet 2? 如何连接工作表1中的两行并将其粘贴到工作表2中的任何列中?

For example, both the columns are numbers, 例如,两列都是数字,

In Sheet1, column A has 123456, column B has 1 在工作表1中,列A具有123456,列B具有1

I want output on Sheet2 column C as 1234561 我想在Sheet2列C上输出为1234561

Please help, thanks!! 请帮忙,谢谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将特定范围的 Excel 单元格从一个工作表复制到另一个工作表 - copying of specific range of excel cells from one worksheet to another worksheet VBA Excel将公式范围从一个工作表复制到另一个工作表 - VBA Excel copying range of formulas from one worksheet to another 在 Excel 中,将某个特定单元格从一个工作表复制到另一个工作表的 VBA 可能是什么? - What could be the VBA for copying some specific cell from one worksheet to another in Excel? EXCEL VBA-将符合条件的列从一个工作表复制到格式化的工作表 - EXCEL VBA - Copying columns that match a condition from one worksheet to a formatted worksheet 将整个工作表从一个Excel复制到另一个 - Copying Entire Worksheet from one Excel to another Excel宏:根据条件将行值从一个工作表复制到另一个工作表中的特定位置 - Excel Macro: Copying row values from one worksheet to a specific place in another worksheet, based on criteria 将特定范围从一个工作表复制到另一工作表 - Copying a specific range from one worksheet to another worksheet 如何使用VBA在excel上将列从一个工作表复制到另一个工作表? - How to copy columns from one worksheet to another on excel with VBA? 使用VBA将一个Excel工作表的格式复制到另一个Excel工作表 - Copying Format of one excel sheet to another excel worksheet using VBA 将特定列从excel工作表复制到另一个工作表 - Copy specific columns from excel worksheet to another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM