有人知道如何在excel中的两个文档之间发送和接收数据吗? File_1中的每一行都有File_2的输入和输出。
只使用一次就没问题了
=[File_2.xlsx]Sheet!$A$1
,但我无法弄清楚如何逐行完成。
File_1
这是我的列表,包含来自File_2的输入和结果
编辑:File_2是一个大的excel文件,一端是手动输入,另一端是输出,此时手动复制到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
输入的output_col_n output_col_n = input_col_n * 3.14
答案 0 :(得分:2)
假设你不想在File_2上做很多工作(你称之为"黑盒子"),最简单的选择是编写一个宏,输入列并填充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
(当然,你需要修改路径和单元/表格参考 - 最好制作这些定义的常量)。
答案 1 :(得分:1)
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
我没有明确地对此进行测试,但它应该可行。