簡體   English   中英

創建一個Excel宏,該宏將搜索標題並復制並粘貼該列

[英]Create an Excel macro which searches a heading and copy-paste the column

我是Excel宏的新手。 我有一些標題分散在許多工作表中的列。 我想在具有光標的某些列中鍵入標題,並將具有該標題的列復制粘貼到具有光標的列中。

可以通過錄制宏來做到這一點嗎? 怎么樣? 如果沒有,我該如何編程?

以下是有關復制找到的列的代碼的一些說明。

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

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

如果('columnheading'='column to check','line1ref','line1refine other sheet')

您將必須檢查每一列來執行此操作。

是否有很多工作表,各列是否始終位於同一位置?

暫無
暫無

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

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