2012-07-06 71 views
1

新的一天,新的問題。 我收到了很多幫助,並希望你也可以幫助我。已經四處搜尋解決我的問題,但沒有找到。使用列索引,如何複製特定列

這裏是我的代碼:

SearchString = "start" 
    Set aCell = phaseRange.Find(What:=SearchString, LookIn:=xlValues, _ 
      LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False) 
If Not aCell Is Nothing Then 
    Set bCell = aCell 
    ReDim Preserve arrStart(nS) 
    arrStart(nS) = aCell.Row 
    nS = nS + 1 
    Do While ExitLoop = False 
     Set aCell = phaseRange.FindNext(After:=aCell) 
     If Not aCell Is Nothing Then 
      If aCell.Row = bCell.Row Then Exit Do 
      ReDim Preserve arrStart(nS) 
      arrStart(nS) = aCell.Row 
      nS = nS + 1 
     Else 
      ExitLoop = True 
     End If 
    Loop 
Else 
End If 

而且寫出來:

For i = 1 To nS - 1 
     Sheets("DataSheet").Select 
     Sheets("raw_list").Rows(arrStart(i)).Copy 
     Cells(i, 1).Select 
     ActiveSheet.Paste 
Next 

正如你看到的,我在一弦一柱尋求。當我得到結果時,行號存儲在一個數組中。在下一個代碼片段中,我寫出了整行中的命中。是否有可能不是取整行,而是以簡單的方式選擇要複製的行中哪些列?

感激援助

回答

1

如果你已經知道的列數,你必須在數組中的行那就試試這個

Sheets("raw_list").Cells(arrStart(i),5).Copy 

這將細胞從一個第5列(西E)複製特定行。

+0

工程就像一個魅力。謝謝。但試圖添加更多的列。試圖將其改爲.Range,但無法使其工作。 – Andreas 2012-07-06 10:59:11

+0

也嘗試與.Union。但它不起作用 'Sheets(「raw_list」)。Union(Cells(arrStart(i),NameCol),Cells(arrStart(i),PhaseCol))。複製' – Andreas 2012-07-06 13:22:44

+0

使用'範圍'而不是'聯合':) – 2012-07-06 13:42:50