从一个excel到另一个excel的交互式复制粘贴

时间:2018-01-10 14:11:04

标签: excel vba

我使用以下代码将内容从一个excel复制粘贴到另一个excel。但是没有数据被复制。 请帮忙。

Sub foo3()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant

path1 = Application.GetOpenFilename()
path2 = Application.GetSaveAsFilename()

'## Open both workbooks first:
Set x = Workbooks.Open(path1)
Set y = Workbooks.Open(path2)

'Store the value in a variable:
vals = x.Sheets("sheetname").Range("B1:B6").Value

'Use the variable to assign a value to the other file/sheet:
y.Sheets("sheetname").Range("A1:A6").Value = vals

'Close x:
x.Close

End Sub

1 个答案:

答案 0 :(得分:-1)

Sub foo3()

Dim x as Workbook
Dim y as Workbook
Dim xName as String
Dim yName as String

xName = Application.GetOpenFileName()
yName = Application.GetOpenFileName()

Set x = Workbooks.Open(xName)
Set y = Workbooks.Open(yName)

y.Sheets("sheetname").Range("A1:A6").Value = x.Sheets("sheetname").Range("B1:B6").Value

x.Close

End Sub

我希望这有效。

否则,试试这个:

Sub foo3()

Dim x as Workbook
Dim y as Workbook
Dim xName as String
Dim yName as String

xName = Application.GetOpenFileName()
yName = Application.GetOpenFileName()

Set x = Workbooks.Open(xName)
Set y = Workbooks.Open(yName)

x.Sheets("sheetname").Range("B1:B6").Cells.Copy y.Sheets("sheetname").Range("A1:A6").Cells.PasteSpecial xlPasteAll

x.Close

End Sub