[英]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)
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.