需要打开两个excel文件,并使用vba将数字添加到第三个文件中

问题描述:

我有两个excel文件,它们具有相似的格式和数据映射表b15:h31。行15是标题,列B也是。我想逐个读取file1,并将该单元格的内容添加到文件2中的相应单元格,即将文件1中的C16添加到文件2中的C16,将文件1中的C17添加到C17在文件2中等等。输出到文件3或任何东西。试图通过vba实现,但迄今为止没有成功。有谁知道如何去做。需要打开两个excel文件,并使用vba将数字添加到第三个文件中

我不知道确切的编码,但我想:

open both files to read from 
open 3rd file to write to 
read in the first line from the first and second file 
split these two lines with the separator used into an array of values 
     (for instance I seperate cells with a comma when writing to an excel file) 
go through these array (this would equate to going through a row in both files) 
    foreach value in the array add the value from the first with the value from the second 
    print it to the 3rd file and the print the cell separator (comma) 
go on to the next value until you reach the end of the 'row' 
print a newline character into the 3rd file to go to the next row 
then read the next line of the first and second file, repeat til done reading files 
close the input and output files 

凯拉已经明确了你的逻辑。如果你正在寻找的语法,使用

Application.Workbooks.Open ("file2.xls") 
Application.Workbooks.Open ("file3.xls") 

打开,

Workbooks("file3.xls").Activate 

激活工作簿要读取或写入,然后

Workbooks("file3.xls").Activate 
Workbooks("file3.xls").Save 
Workbooks("file3.xls").Close 

以保存并关闭。