vba 将某些单元格复制到新的工作簿/工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10305623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:55:13  来源:igfitidea点击:

Copy certain cells to a new workbook/sheet

excelvbaexcel-vbaexcel-2003

提问by nightrunner

I've had a thorough search through previous questions, but I can't find quite what I'm looking for.

我对以前的问题进行了彻底的搜索,但找不到我正在寻找的内容。

Daily I have to download a file from the internet, the columns of which are always the same, but the values (in all but the title row) are the different. There are many columns which I don't need to pay attention to.

每天我都必须从互联网上下载一个文件,其中的列总是相同的,但是值(除了标题行之外的所有值)都不同。有很多列我不需要关注。

I'd like to be able to create a macro that I will link to a button in Excel that will: Take certain columns from one sheet, and paste them to a new blank sheet or workbook.

我希望能够创建一个宏,我将链接到 Excel 中的一个按钮,该按钮将: 从一张工作表中取出某些列,并将它们粘贴到新的空白工作表或工作簿中。

Most of the results I've found when searching seem to contain rules: "if something", or "when ...", etc. - I don't need this.

我在搜索时发现的大多数结果似乎都包含规则:“如果某事”或“何时……”等 - 我不需要这个。

I'm just trying to make something that will take: Column: C, N, AC, AG, AZ, etc. and paste them to column A, B, C, D, etc. in another blank sheet.

我只是想制作一些需要的东西:列:C、N、AC、AG、AZ 等,然后将它们粘贴到另一张空白表中的 A、B、C、D 列等。

回答by Matt Robinson

Fortunately, writing the code to do that is pretty simple. Just create a button and a click method, and then put something like this into the body of the method:

幸运的是,编写代码来做到这一点非常简单。只需创建一个按钮和一个 click 方法,然后将这样的内容放入方法体中:

Sheets("Sheet3").Select
Columns("A:A").Select
Selection.Copy
Sheets("Sheet4").Select
Range("A1").Select
ActiveSheet.Paste

You'll need to do the meat of this for each column, but if you create a nice UI for the user, it could all be done in code.

您需要为每一列做这些工作,但是如果您为用户创建了一个漂亮的 UI,那么这一切都可以在代码中完成。