2008-12-30 138 views
0

我是新的Excel宏。我有一些標題分散在許多工作表中的列。我想在某個具有光標的列中鍵入一個標題,並將具有該標題的列複製粘貼到具有光標的列。創建Excel宏,其中搜索標題並複製粘貼列

是否有可能通過錄制宏來實現這一點?怎麼樣?如果沒有,我該如何編程?

+0

您是否正在尋找自動完成新標題?或者將該特定列中的所有值複製到新列? – Dheer 2008-12-30 11:13:30

+0

兩種解決方案都可以接受。自動完成甚至似乎更好;我不知道這甚至是一種選擇。 – namin 2008-12-30 11:30:21

回答

1

以下是關於複製找到的列的代碼的一些注意事項。

Dim ws As Worksheet 
Dim r As Range 
Dim CopyTo As String 

'You must run the code from a suitable active cell ' 
strFind = ActiveCell.Value 
'Assumes the column data will be written into the next row down ' 
CopyTo = ActiveCell.Offset(1, 0).Address 

'Look at each worksheet' 
For Each ws In ActiveWorkbook.Worksheets 
    'Skip the active worksheet ' 
    If ws.Name <> ActiveSheet.Name Then 
     'Get the last cell and row' 
     lc = ws.Cells.SpecialCells(xlCellTypeLastCell).Column 
     lr = ws.Cells.SpecialCells(xlCellTypeLastCell).Row 

     'Use the first row for search range ' 
     Set r = ws.Range(ws.Cells(1, 1), ws.Cells(1, lc)) 

     'Find the first whole cell to match the active cell ' 
     Set f = r.Find(strFind, , , xlWhole) 
     'If it is found ... ' 
     If Not f Is Nothing Then 
      'Copy the whole column to the copy position ' 
      ws.Range(ws.Cells(2, f.Column), _ 
      ws.Cells(lr, f.Column)).Copy (ActiveSheet.Range(CopyTo)) 
     End If 
    End If 
Next 
0

標題是否會在不同的工作表中出現多次?如果沒有,那麼我建議使用一個簡單的if語句

如果(「columnheading」 =「欄來查看」,「line1ref」,「line1refin其他表」)

您必須檢查每列做這個。

有很多牀單,列是否總是在同一個位置?