将数据从一个打开的工作簿传输到另一个打开的工作簿

时间:2021-07-05 20:07:09

I have two open workbooks, Workbook A and Workbook B .I have columns of data that I need to transfer from a specific sheet from Workbook A to a specific sheet in Workbook B.

我有两个打开的工作簿,即工作簿A和工作簿B.我需要将数据列从特定工作表A从工作簿A传输到工作簿B中的特定工作表。

Sample data:

-26.32
-20.56
-44.93
-26.64
-45.99
-40.76
-34.21
-25.54
-48.63    
2.966    
9.163    
4.879    
5.01    
11.48    
16.19    
16.19

How to create a program that transfers this from Workbook A to Workbook B? Also Workbook A can be any open workbook. It might not be the same file when this program runs again. Thanks in advance.

如何创建一个程序,将其从工作簿A转移到工作簿B?工作簿A也可以是任何打开的工作簿。当该程序再次运行时,它可能不是同一个文件。提前致谢。

Current Code:

Sub Sample()

Dim wbA As Workbook, wbB As Workbook
Dim filepath As String

Set wbA = ThisWorkbook 'you say the current workbook
filepath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", , , , False)

Set wbB = Workbooks.Open(filepath)

With wbA
    .Sheets("Results").Range("A1").Copy wbB.Sheets("Sheet1").Range("A1")
End With

End Sub

Choose Source and Destination Workbook:

选择源和目标工作簿:

Sub Sample()

Dim wbA As Workbook, wbB As Workbook
Dim filepath As String

filepath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", , , , False)
Set wbA = Workbooks.Open(filepath)

filepath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", , , , False)
Set wbB = Workbooks.Open(filepath)

With wbA
    .Sheets("Sheet1").Range("A1:A3").Copy wbB.Sheets("Sheet1").Range("A1:A3")
End With

End Sub

1 个解决方案

#1


0  

Try this:

 Sub Sample()

 Dim wbA as  Workbook, wbB as Workbook
 Dim filepath as String

 Set wbA = Thisworkbook 'you say the current workbook
 filepath = "C:\User\UserName\Documents\Myworkbook.xlsx" 'hard coded version

 Set wbB = Workbooks.Open(filepath)

With wbB
    .Sheets("your sheet name here").Range("your range here").Copy _
    wbA.Sheets("destination sheet here").Range("destination range here")
End With

End Sub

If however you want to select your file, use this line:

但是,如果要选择文件,请使用以下行:

filepath = Application.GetOpenFilename("Excel Files (*.xlsx), "*.xlsx",,,,False) 'open file dialog version

Then you got to select the file from the location.
Hope this helps a bit.

然后你必须从该位置选择文件。希望这个对你有帮助。

Edit1: Try this

编辑1:试试这个

filepath = Application.GetOpenFilename(filefilter:="Excel Files (*.xlsx), "*.xlsx") 'open file dialog version

#1


0  

Try this:

 Sub Sample()

 Dim wbA as  Workbook, wbB as Workbook
 Dim filepath as String

 Set wbA = Thisworkbook 'you say the current workbook
 filepath = "C:\User\UserName\Documents\Myworkbook.xlsx" 'hard coded version

 Set wbB = Workbooks.Open(filepath)

With wbB
    .Sheets("your sheet name here").Range("your range here").Copy _
    wbA.Sheets("destination sheet here").Range("destination range here")
End With

End Sub

If however you want to select your file, use this line:

但是,如果要选择文件,请使用以下行:

filepath = Application.GetOpenFilename("Excel Files (*.xlsx), "*.xlsx",,,,False) 'open file dialog version

Then you got to select the file from the location.
Hope this helps a bit.

然后你必须从该位置选择文件。希望这个对你有帮助。

Edit1: Try this

编辑1:试试这个

filepath = Application.GetOpenFilename(filefilter:="Excel Files (*.xlsx), "*.xlsx") 'open file dialog version