I'm very new to vba programming and I would like to know what is the best method to do the following operation : read data from two Excel files (from a particular column of each file) and write this data in a third Excel file, writing the data contained in the first file first then the data contained in the second file. The figure below illustrates what I want to do :
我是vba编程的新手,我想知道执行以下操作的最佳方法是什么:从两个Excel文件(从每个文件的特定列)读取数据并将此数据写入第三个Excel文件,首先写入第一个文件中包含的数据,然后写入第二个文件中包含的数据。下图说明了我想要做的事情:
How can I do that on click on a command button, without opening the two first files (I mean I don't want the user to see them).
如何在单击命令按钮的情况下执行此操作,而无需打开两个第一个文件(我的意思是我不希望用户看到它们)。
1 个解决方案
#1
You need to make so
你需要这样做
Workbooks.Open Filename:= "c:\myfile\FILE1.xls"FILE1.xls"
Workbooks.Open Filename:= "c:\myfile\FILE2.xls"
After you open the FILE1.xls and FILE.xls you can create or open FILE3.xls, in this case we create the FILE3.xls
打开FILE1.xls和FILE.xls后,您可以创建或打开FILE3.xls,在这种情况下,我们创建FILE3.xls
Dim wb As Workbook
Set wb = Workbooks.Add
After this you read the data in FILE1.xls and FILE2.xls, you can read with WHILE or other LOOP or you read with COPY and PASTE data in new file. In this case we see the LOOP strategy.
在此之后,您可以读取FILE1.xls和FILE2.xls中的数据,您可以使用WHILE或其他LOOP读取,或者使用新文件中的COPY和PASTE数据进行读取。在这种情况下,我们看到了LOOP策略。
Dim i as Integer
Dim value as String
i=0
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Workbooks("FILE1.xls").Worksheets("sheet1").Activate
while value <> ""
Workbooks("FILE1.xls").Worksheets(2).Range("A:A").Cells(i, 1) = value
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Wend
Repeat ste for "FILE2.xls" read data and write in "FILE3.xls" and at the and you close
对“FILE2.xls”重复读取数据,然后在“FILE3.xls”中写入,然后关闭
wb.SaveAs "c:\myfile\"FILE3.xls""
wb.Close
And close the read data file
并关闭读取数据文件
Workbooks("FILE1.xls").Close
Workbooks("FILE2.xls").Close
#1
You need to make so
你需要这样做
Workbooks.Open Filename:= "c:\myfile\FILE1.xls"FILE1.xls"
Workbooks.Open Filename:= "c:\myfile\FILE2.xls"
After you open the FILE1.xls and FILE.xls you can create or open FILE3.xls, in this case we create the FILE3.xls
打开FILE1.xls和FILE.xls后,您可以创建或打开FILE3.xls,在这种情况下,我们创建FILE3.xls
Dim wb As Workbook
Set wb = Workbooks.Add
After this you read the data in FILE1.xls and FILE2.xls, you can read with WHILE or other LOOP or you read with COPY and PASTE data in new file. In this case we see the LOOP strategy.
在此之后,您可以读取FILE1.xls和FILE2.xls中的数据,您可以使用WHILE或其他LOOP读取,或者使用新文件中的COPY和PASTE数据进行读取。在这种情况下,我们看到了LOOP策略。
Dim i as Integer
Dim value as String
i=0
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Workbooks("FILE1.xls").Worksheets("sheet1").Activate
while value <> ""
Workbooks("FILE1.xls").Worksheets(2).Range("A:A").Cells(i, 1) = value
value = value = Workbooks("FILE1.xls").Worksheets("sheet1")..Range("B:B").Cells(i, 1)
Wend
Repeat ste for "FILE2.xls" read data and write in "FILE3.xls" and at the and you close
对“FILE2.xls”重复读取数据,然后在“FILE3.xls”中写入,然后关闭
wb.SaveAs "c:\myfile\"FILE3.xls""
wb.Close
And close the read data file
并关闭读取数据文件
Workbooks("FILE1.xls").Close
Workbooks("FILE2.xls").Close