我想编写一个宏,将选定范围的单元格从一个Excel文件复制到另一个Excel文件。我试过那段代码,但它不起作用。我看到的唯一动作是wbk和wbk 1工作簿是打开的,但其余代码没有执行,所以我不能复制我需要的东西。有什么想法是错的吗?
Sub new()
Dim wbk As Workbook
Dim wbk1 As Workbook
Set wbk = Workbooks.Open("C:\Users\Marcin\Desktop\plik zrodkowy.xlsm")
Set wbk1 = Workbooks.Open("C:\Users\Marcin\Desktop\Zeszyt2.xlsm")
' now you can manipulate the data in the workbook anyway you want, e.g. '
Dim x, y As Variant
x = wbk.Worksheets("Sheet1").Range(Cells(1, 1), Cells(13, 2))
y = wbk1.Worksheets("Sheet1").Range(Cells(1, 20), Cells(13, 23))
答案 0 :(得分:1)
感谢大家的帮助,最后我编辑了我的代码,现在它可以工作,就像我需要的,这里是我的鳕鱼,如果它可以帮助将来的任何人:)我是波兰的开发人员而不是“Arkusz1”你应该如果您使用的是英文版的Excel,请使用“Sheet1”。关心Marcin
Dim strPath2 As String
Dim wbkWorkbook1 As Workbook
Dim wbkWorkbook2 As Workbook
Dim wbThis As Workbook
'define paths and filenames
strPath2 = "C:\Users\Marcin\Desktop\plik zrodkowy.xlsm"
Set wbThis = ThisWorkbook
'open files
Set wbkWorkbook2 = Workbooks.Open(strPath2)
Set Workbook1 = ThisWorkbook
'copy the values across
'### change the sheet and range to what you need
'wbkWorkbook2.Worksheets("Arkusz1").Range("A1:B3").Value = _
wbkWorkbook1.Worksheets("Arkusz1").Range("A1:B3").Value'
wbThis.Worksheets("Arkusz1").Range("E1:F3").Value =
wbkWorkbook2.Worksheets("Arkusz1").Range("A1:C3").Value
'close the workbook
wbkWorkbook2.Close (True)
答案 1 :(得分:0)
您的代码缺少任务。第一部分是打开工作簿,然后声明并设置变量,但是对它们不做任何操作。如果要复制范围,请添加以下代码行:
x.Copy (y)