将两列从一个Excel工作表复制到另一个 [英] Copy two columns from one Excel sheet to another

查看:107
本文介绍了将两列从一个Excel工作表复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用VBScript将一个Excel工作表的两列复制到另一Excel工作表的同一列.这些列彼此相邻我正在使用以下代码,这些代码是通过一些搜索得到的,并已更改为我的需要.如果要复制一列,而不是将两列从一个Excel复制到另一列,则效果很好.

I need to copy two columns of one Excel sheet to the same columns of another Excel sheet using VBScript. These columns are adjacent to each other I am using the following code, which I got from by some search and changed to my need. It is working fine if it is to copy one column but it is not copying two columns from one excel to another.

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Desktop\Customer.xlsx")
Set objWorkbook2 = objExcel.Workbooks.Open("C:\Documents\Folder1\Test.xlsx")

Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate

Set objRange = objWorkSheet.Range("A1").EntireColumn
Set objRange = objWorkSheet.Range("B1").EntireColumn
objRange.Copy

Set objWorksheet2 = objWorkbook2.Worksheets(1)
objWorksheet.Activate

Set objRange = objWorkSheet2.Range("A1")
Set objRange = objWorkSheet2.Range("B1")
objWorksheet.Paste(objRange)

我的输入Excel文件是:

My Input Excel file is:

Customer Number    Customer Name
1001    Wendy
1002    Subway
1003    McDonalds

我的输出Excel文件具有相同的列名,但目前在这些列中没有值.从输入的Excel复制后,第二个Excel文件中的这些列应具有相同的值.

My output Excel file has the same column names but is presently have no values in these columns. After copying from the input Excel, I should have the same values to the these columns in the second Excel file.

推荐答案

Set objRange = objWorkSheet.Range("A1").EntireColumn
Set objRange = objWorkSheet.Range("B1").EntireColumn

以上两个语句首先选择列A,然后选择列B.要选择2列,请使用以下内容:

The above two statements first select column A, then select column B. To select 2 columns use something like this:

Set objRange = objWorkSheet.Range("A:B").EntireColumn

您甚至不需要将所选内容分配给变量.只需在对象上调用方法:

You don't even need to assign the selection to a variable. Simply call the methods on the objects:

objWorkSheet.Range("A:B").EntireColumn.Copy
objWorkSheet2.Paste objWorkSheet2.Range("A1")

这篇关于将两列从一个Excel工作表复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆