簡體   English   中英

將excel行從一個工作表復制到另一個工作表

[英]Copying excel row from one sheet to another

我將數據從一個工作表復制到另一個工作表,從源范圍(Q5:AIxxx)到目標范圍(C6:Uxxx)

我的VBA代碼以單元格開頭並循環到列的末尾以完成復制該列的數據:

Set s = Worksheets("Source")
Set d = Worksheets("Destination")

Dim i As Integer, j As Integer

j = 6
For i = 5 To 1500
   If s.Cells(i, 1).Value = "a" Or s.Cells(i, 1).Value = "b" Then
      d.Cells(j, 3).Value = s.Cells(i, 17).Value       
      j = j + 1
   End If
Next i

我有> 20列移動,有沒有辦法可以立即復制行? 這樣的事情:

d.Cells(j, 3:21).Value = s.Cells(i, 17:35).Value     

目前,我必須指定每一列:

 d.Cells(j, 3).Value = s.Cells(i, 17).Value    'column 1
 d.Cells(j, 4).Value = s.Cells(i, 18).Value    'column 2
 d.Cells(j, 5).Value = s.Cells(i, 19).Value    'column 3
 etc

您可以使用:

.Range(<your_range>).copy

比如做:

Activesheet.Range("A1:B10").Copy

然后用你想要的任何地方粘貼.Paste

ActiveSheet.Paste Destination:=Worksheets(2).Range("D1:D5")

它是vba中的內置功能。 另見復制范圍:

https://msdn.microsoft.com/en-us/library/office/ff837760.aspx

對於粘貼: https//msdn.microsoft.com/en-us/library/office/ff821951.aspx

你可以做到

    d.range(d.cells(j,3), d.cells(j,21)).copy
 s.range(s.cells(i,17), s.cells(i,35)).pastespecial xlvalues

使用范圍可以使它更容易:

Dim s As Range, d As Range
Set d = Worksheets("Destination").Cells(6, 15) ' notice the column 15

For Each s in Worksheets("Source").Range("A5:A1500")
   If s = "a" Or s = "b" Then
      d(,3) = s(,3)
      d(,4) = s(,4)
      ' or d(,3).Resize(,19) = s(,3).Resize(,19) ' .Value optional
      Set d = d.Offset(1) ' move to next row
   End If
Next 

沒有VBA,有更簡單的方法可以做到這一點。 例如Power Query,PivotTable,Copy Link和Table Filter,Data選項卡> From Access等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM