2010-06-15 56 views
0

我有兩個excel文件,它們具有相似的格式和數據映射表b15:h31。行15是標題,列B也是。我想逐個讀取file1,並將該單元格的內容添加到文件2中的相應單元格,即將文件1中的C16添加到文件2中的C16,將文件1中的C17添加到C17在文件2中等等。輸出到文件3或任何東西。試圖通過vba實現,但迄今爲止沒有成功。有誰知道如何去做。需要打開兩個excel文件,並使用vba將數字添加到第三個文件中

回答

1

我不知道確切的編碼,但我想:

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 
1

凱拉已經明確了你的邏輯。如果你正在尋找的語法,使用

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 

以保存並關閉。

相關問題