0

我需要一种将“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

请帮忙!

//狮子座

4

1 回答 1

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
于 2013-04-23T11:02:31.100 回答