vba 将列从一个 Excel 工作表复制到另一个,跳过之间的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9605194/
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:28:30  来源:igfitidea点击:

Copy columns from one Excel sheet to another, skipping columns in between

excelvbaexcel-vbaexcel-2007

提问by Nupur

Possible Duplicate:
Copy column from one Excel sheet to another Excel sheet

可能的重复:
将列从一个 Excel 工作表复制到另一个 Excel 工作表

I have 5 sheets in my Excel file. Out of these 5, one is the master sheet which has all the data I want to copy to the other 4 sheets. All the sheets have one common column, ID. The master has more than 10000 IDs.

我的 Excel 文件中有 5 张纸。在这 5 个中,一个是主表,其中包含我想复制到其他 4 个表的所有数据。所有工作表都有一个公共列 ID。主人有10000多个ID。

What I want to do is:

我想做的是:

If ID in sheet1 = 24356 = ID in master sheet then copy x, y, z columns from master sheet to sheet1. This is same for all other sheets.

如果 sheet1 中的 ID = 24356 = 母版中的 ID,则将 x、y、z 列从母版复制到 sheet1。这对于所有其他工作表都是一样的。

Also, since the master sheet is from another source than the rest of the sheets, its formatting is different. Is there a way to remove all formatting in sheets before running copy/paste?

此外,由于母版工作表与其他工作表的来源不同,因此其格式不同。有没有办法在运行复制/粘贴之前删除工作表中的所有格式?

Can anyone please tell me the VBA code to do this.

任何人都可以告诉我执行此操作的 VBA 代码。

This is what my Master sheet looks like:

这是我的主表的样子:

enter image description here

在此处输入图片说明

I want the other sheet (e.g. sheet1) in this case to look like:

在这种情况下,我希望另一张纸(例如 sheet1)看起来像:

enter image description here

在此处输入图片说明

Also, while searching the master sheet, is it possible for the code to look through the last column of the sheet?

另外,在搜索母版时,代码是否可以查看工作表的最后一列?

回答by Alex P

Nupur

努普尔

Here is the non-VBA way.

这是非VBA方式。

Assume your master data is on a sheet called Masterand the data is in range A1:H9. Now assume I have Sheet1with a list of IDs in range A2:A5.

假设您的主数据在一个名为的工作表上Master并且数据在 range 中A1:H9。现在假设我有Sheet1一个范围内的 ID 列表A2:A5

 1   ID    Name    Type    Question1    Type3    Type4
 2   475
 3   479
 4   501
 5   503

Then in Sheet1in cell B2I have the following to retrieve Namefor ID=475:

然后在Sheet1单元格中,B2我要检索Name以下内容ID=475

=VLOOKUP($A2,Master!$A:$H,2,0) //returns f1

To get Typefor ID=475I use:

为了得到TypeID=475我使用:

=VLOOKUP($A2,Master!$A:$H,3,0) //returns adm1

All I am doing is setting up a reference to the data table on Master(note absolute ref with $ signs), using ID as the lookup value, and then changing the column value (3rd input in VLOOKUP) to retreive the relevant value.

我所做的只是设置对数据表的引用Master(注意带有 $ 符号的绝对引用),使用 ID 作为查找值,然后更改列值(VLOOKUP 中的第三个输入)以检索相关值。

Once you have set this up for the first row you can then just copy down and get the right data populated.

一旦你为第一行设置了这个,你就可以向下复制并填充正确的数据。

Does this solve the issue?

这能解决问题吗?