用于将单元格内容复制到另一个工作簿中的特定工作表的Excel宏

时间:2013-04-23 10:35:43

标签: excel-vba vba excel

我需要的是一种方法将“ThisWorkbook”(宏的位置)中的某些单元格的内容发送到另一个工作簿中的特定工作表(其位置不会改变,与“ThisWorkbook”不同)

出于某种原因,下面的内容不起作用:

Sub Transplant()

Dim thispath As String
Dim targetpath As String

'Set filepaths
thispath = ThisWorkbook.FullName
targetpath = ThisWorkbook.Path & "/subdir/Targetbook.xlsm"



Dim Srcwb As Workbook
Dim Trgwb As Workbook

'Set workbooks
Set Srcwb = Workbooks.Open(thispath)
Set Trgwb = Workbooks.Open(targetpath)

Srcwb.Worksheets("Sheet1").Range(Srcwb .Worksheets("Sheet1").Range("A1"), _
Srcwb.Worksheets("Sheet1").Range("A1").End(xlToRight)).Copy _
Destination:=Trgwb.Sheets("Sheet1").Cells(1, 1)


End Sub

请帮忙!

//利奥

1 个答案:

答案 0 :(得分:0)

这与你所拥有的几乎相同,虽然我没有重新打开活动工作簿。

您能描述一下您要复制的范围吗?您可能会发现UsedRange更容易。

Sub Transplant()
    Dim DWB As Workbook
    Dim S As Worksheet
    Set S = ThisWorkbook.WorksheetS("Sheet1") ' forgot to rename Source to S

    Set DWB = Application.Workbooks.Open(Thisworkbook.Path & "/subdir/Targetbook.xlsm")
    Set D = DWB.Worksheets("Sheet1")

    S.Range(S.Range("A1"), S.Range("A1").End(xlToRight)).Copy Destination:=D.Cells(1,1)
    ' S.UsedRange.Copy Destination:=D.Cells(1,1) - this might be easier
End Sub