创建Excel宏,其中搜索标题并复制粘贴列
问题描述:
我是新的Excel宏。我有一些标题分散在许多工作表中的列。我想在某个具有光标的列中键入一个标题,并将具有该标题的列复制粘贴到具有光标的列。创建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” =“栏来查看”,“line1ref”,“line1refin其他表”)
您必须检查每列做这个。
有很多床单,列是否总是在同一个位置?
您是否正在寻找自动完成新标题?或者将该特定列中的所有值复制到新列? – Dheer 2008-12-30 11:13:30
两种解决方案都可以接受。自动完成甚至似乎更好;我不知道这甚至是一种选择。 – namin 2008-12-30 11:30:21