热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

创建一个Excel宏,用于搜索标题并复制粘贴列-CreateanExcelmacrowhichsearchesaheadingandcopy-pastethecolumn

IamnewtoExcelmacros.Ihavesomecolumnswithheadingsscatteredinmanysheets.Iwouldliket

I am new to Excel macros. I have some columns with headings scattered in many sheets. I would like to type a heading in some column which has the cursor and have the column with that heading copy-pasted to the column with the cursor.

我是Excel宏的新手。我有一些标题分散在许多表格中的列。我想在某个列中键入一个带有光标的标题,并将带有该标题的列用光标复制粘贴到该列。

Is it possible to do this by recording a macro? How? If not, how do I do it programmatically?

是否可以通过录制宏来完成此操作?怎么样?如果没有,我该如何以编程方式进行?

2 个解决方案

#1


Here are a few notes on code to copy a found column.

以下是有关复制已找到列的代码的一些注释。

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

#2


Does the heading appear many times across the different sheets? If not, then I would suggest using a simple if statement

标题是否在不同的纸张上出现多次?如果没有,那么我建议使用简单的if语句

if ('columnheading' = 'column to check','line1ref', 'line1refin other sheet')

if('columnheading'='列检查','line1ref','line1refin其他表')

You will have to check each column to do this.

您必须检查每列才能执行此操作。

Are there many sheets, are the columns always in the same position?

有多张纸,列总是在同一个位置吗?


推荐阅读
author-avatar
墨鱼轩869
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有