Excel:将行发送到其他文档并逐行接收输出?

有谁知道如何发送和接收两个Excel文件之间的数据? File_1中的每一行都有来自File_2的input和输出。

只要做一次是没有问题的使用=[File_2.xlsx]Sheet!$A$1 ,但我不知道如何做到一行一行。

File_1

这是来自File_2的input和结果的列表

编辑:File_2是一个大型的Excel文件,一端是手动input,另一端是手动input到File_1的输出。 File_2中没有对File_1的引用。

 input_col_1 output_col_1 input_col_2 output_col_2 input_col_3 output_col_3 

File_2

这是一个黑盒计算器,其中一个单元格包含使用来自File_1的input的output_col_n

output_col_n = input_col_n * 3.14

假设你不想在File_2上做很多工作(你称之为“黑匣子”),最简单的select是编写一个macros,它沿着你的input列向下input,并在File_1上填充你的输出列:

 Public Sub getOutputFromFile_2() Dim lCurrRow As Long Dim wbFile_2 As Workbook 'Check if File_2 is already open For Each wbFile_2 In Workbooks If wbFile_2.FullName = "D:\File_2.xlsx" Then Exit For Next If wbFile_2.FullName <> "D:\File_2.xlsx" Then Set wbFile_2 = Workbooks.Open("D:\File_2.xlsx") 'if not, then open it 'populate the output column in this file For lCurrRow = 1 To 5 With ThisWorkbook.Worksheets(1) wbFile_2.Worksheets(1).Range("A1").Value = .Cells(lCurrRow, 1).Value .Cells(lCurrRow, 2).Value = wbFile_2.Worksheets("Sheet6").Range("C5").Value End With Next lCurrRow End Sub 

(你将需要修改当然的path和单元格/工作表引用 – 这将是一个很好的做法,使这些定义的常量)。

 Sub filetransfer() Dim File1 As Workbook Dim File2 As Workbook Dim File1Input As Range Dim File1Output As Range Dim File2Input As Range Dim File2Output As Range Dim i As Integer Dim iOffset As Integer Set File1 = Application.Workbooks("FILEPATH\File1.xlsx") ' Alter file path and excel file name Set File2 = Application.Workbooks("FILEPATH\File2.xlsx") ' Alter file path and excel file name ' Turn off screen updating and events for easier execution Application.ScreenUpdating = False Application.EnableEvents = False ' Open both workbooks Application.Workbooks.Open (File1) Application.Workbooks.Open (File2) ' Set Range of data you want to take out of File1 Set File1Output = File1.Sheets("1").Range("A1:A3") ' Replace Sheet(1) with sheet name you want and Set your actual range ' Set Range in File 2 where File 1 data will go Set File2Input = File2.Sheets("1").Range("A1:A3") ' Replace sheet and range for your data/setup ' Set Range in File 1 where File 2 data will go Set File1Input = File1.Sheets("1").Range("A1:A3") ' Replace sheet and range ' Set Range of data you want to take out of File2 Set File2Output = File2.Sheets("1").Range("A1:A3") ' Set our row number integer i = 1 ' Start stepping through each row of data in File 1 output range For Each r In File1Output.Rows ' copy the data and paste it to File2 Input Range r.Copy File2Input(i, 1).PasteSpecial Paste:=xlPasteValues ' This does a paste on A1 with i = 1. If i = 2 this will paste on A2 iOffset = i + 1 i = iOffset Next r ' You can do more stuff below this like use another loop for copying from file 2 to file 1. ' You can also perform calculations before copying or pasting if necessary. ' Or you could paste in a different format, or paste a formula ' Pasting a formula would require a nested loop to handle each cell within each row of the range ' Make sure you turn screen updating and events back on Application.EnableEvents = True Application.ScreenUpdating = True End Sub 

我没有明确地testing,但它应该工作。