Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:跨工作簿复制粘贴范围对象定义错误_Vba_Excel - Fatal编程技术网

VBA:跨工作簿复制粘贴范围对象定义错误

VBA:跨工作簿复制粘贴范围对象定义错误,vba,excel,Vba,Excel,我无法跨工作簿复制一个范围。也有类似的文章与此相关,但似乎没有一篇对应用程序定义或对象定义的错误有帮助 我试过这个 Set wbSource = Workbooks("Source.xlsx") Set wbTarget = Workbooks("Target.xlsx") Set wbSource_WS = wbSource.Worksheets("Source") Set wbSTarget_WS = wbTarget.Workshee

我无法跨工作簿复制一个范围。也有类似的文章与此相关,但似乎没有一篇对应用程序定义或对象定义的错误有帮助

我试过这个

       Set wbSource = Workbooks("Source.xlsx")
       Set wbTarget = Workbooks("Target.xlsx")
       Set wbSource_WS = wbSource.Worksheets("Source")
       Set wbSTarget_WS = wbTarget.Worksheets("Target")

       wbSource_WS.Activate
       wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
       wbTarget_WS.Activate
       wbSTarget_WS.Range(Cells(Row_TargetStart, Col_TargetStart), Cells(Row_TargetStart, Col_TargetEnd)).PasteSpecial Paste:=xlPasteValues
这也是:

       wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy Destination:= _
       wbSTarget_WS.Range(Cells(Row_TargetStart, Col_TargetStart), Cells(Row_TargetStart, Col_TargetEnd)).PasteSpecial(Paste:=xlPasteValues, Transpose:=True)

您需要完全声明以下范围中的对象:

 wbSource_WS.Activate
 wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
应该是这样的:

 wbSource_WS.Activate
 wbSource_WS.Range(wbSource_WS.Cells(Row_SourceStart, Col_Source), wbSource_WS.Cells(Row_SourceEnd, Col_Source)).Copy
.range中的每个.cell都需要绑定到:wbSource\u WS

使用With语句会更简单/更简洁:

With wbSource_WS
    .Range( .Cells(Row_SourceStart, Col_Source), .Cells(Row_SourceEnd, Col_Source)).Copy
End With

你的代码有错误

Set wbSource = Workbooks("Source.xlsx")
   Set wbTarget = Workbooks("Target.xlsx")
   Set wbSource_WS = wbSource.Worksheets("Source")
   Set wbSTarget_WS = wbTarget.Worksheets("Target")

   wbSource_WS.Activate
   wbSource_WS.Range(Cells(Row_SourceStart, Col_Source), Cells(Row_SourceEnd, Col_Source)).Copy
   **wbSTarget_WS.Activate**
   wbSTarget_WS.Range(Cells(Row_TargetStart, Col_TargetStart), Cells(Row_TargetStart, Col_TargetEnd)).PasteSpecial Paste:=xlPasteValues
我用这个小零钱运行它,它没有产生任何错误


我建议您使用OptionExplicit。这将防止此错误再次发生。Excel会在让您运行VBA程序之前指出存在未声明的变量。

尝试使用此。。。wbSTarget_WS.RangeCellsRow_TargetStart,Col_TargetStart.PasteSpecialPaste:=xlPasteValues,Transpose:=True可能存在源和目标范围大小不匹配的错误…我调试了代码。源和目标范围相同。您可以对所有实例执行此操作;刚才给出了第一个实例作为示例。这可以清除对象定义的错误,但现在出现了一个新错误,无法获取range类的pastespecial属性,我正在尝试修复该错误now@diana您是否尝试过粘贴到不带.Paste Special的范围以查看其是否有效?对于完全定义的范围,您可能只需要选择要粘贴的所需范围的左上角单元格。