簡體   English   中英

使用 VBA 將數據從一張紙復制到另一張紙

[英]Copy data from one sheet to another using VBA

在 excel 2007 中嘗試循環遍歷(非固定長度)列(例如 C)和行值匹配的位置(例如“高”),然后將單元格 Dx 和 Bx 的值轉移到新行中的“轉移”表中,其中 x 是找到匹配項的行號。 假設存在“轉移”。

到目前為止,我有這個:

Public Sub CopyRows()
  Sheets("Sheet1").Select 

  'Find the last row of data
  LastRow = Cells(Rows.Count, 1).End(xlUp).Row

  'Loop through each row
  For x = 1 To FinalRow
    'Decide if to copy based on column C
    ThisValue = Cells(x, 3).Value

    If ThisValue = "High" Then
      Cells(x, 1).Resize(1, 33).Copy
      Sheets("Transfer").Select
      NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
      Cells(NextRow, 1).Select
      ActiveSheet.Paste
      Sheets("Sheet1").Select
    End If
  Next x
End Sub 

我不會那樣做 go 。 我相信首先設置過濾器並僅復制可見行會更快並且更容易編碼。

事實上,最好使用高級過濾器來完成。

創建兩個范圍 - 一個用於條件(標題和單元格):

*C column header* |
-------------------
High              |

一個用於所需數據(僅標題):

*B column header* | *D column header*
-------------------------------------

然后使用

range("A:D").advancedfilter xlFilterCopy, range("criteria range"), range("copy range")

顯然,使用 Excel 接口(數據 - 高級過濾器)更容易做到這一點。

暫無
暫無

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

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