Excel VBA,从非活动工作表获取范围 [英] Excel VBA, getting range from an inactive sheet

查看:318
本文介绍了Excel VBA,从非活动工作表获取范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看Temp表时,此脚本工作正常。但是当我在另一个工作表中时,复制命令失败..它给出了应用程序定义或对象定义的错误

This script works fine when I'm viewing the "Temp" sheet. But when I'm in another sheet then the copy command fails.. It gives a "Application-defined or object-defined error"

Sheets("Temp").Range(Cells(1), Cells(1).End(xlDown)).Copy
Sheets("Overview").Range("C40").PasteSpecial

我可以使用这个脚本,但是我有粘贴问题

I can use this script instead, but then I have problems with pasting it

Sheets("Temp").Columns(1).Copy
Sheets("Overview").Range("C40").PasteSpecial




  • 我不想激活Temp表来获得这个

  • 我还能做什么

    推荐答案

    问题是因为 Cell 中的范围中的引用是不合格的,它们引用默认的 ActiveSheet 。所以你的代码实际上是说

    Your issue is that the because the Cell references inside the Range 's are unqualified, they refer to the default ActiveSheet. So your code is actually saying

    Sheets("Temp").Range(ActiveSheet.Cells(1), ActiveSheet.Cells(1).End(xlDown)).Copy
    Sheets("Overview").Range("C40").PasteSpecial
    

    这是一个更好的版本

    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    
    Set sh1 = ActiveWorkbook.Sheets("Temp")
    Set sh2 = ActiveWorkbook.Sheets("Overview")
    
    With sh1
        .Range(.Cells(1,1), .Cells(1,1).End(xlDown)).Copy
    End With
    sh2.Range("C40").PasteSpecial
    

    这篇关于Excel VBA,从非活动工作表获取范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆