Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Excel ActiveSheet.Paste在其他电脑上不起作用_Excel_Vba - Fatal编程技术网

Excel ActiveSheet.Paste在其他电脑上不起作用

Excel ActiveSheet.Paste在其他电脑上不起作用,excel,vba,Excel,Vba,我使用ctrl+c命令从一个工作表中手动复制一些单元格,并希望使用我创建的宏粘贴它 我有以下代码: Range("A2:W5000").Select Selection.ClearContents Range("A2").Select ActiveSheet.Paste With Selection.Interior .PatternColorIndex = 7 .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.799

我使用ctrl+c命令从一个工作表中手动复制一些单元格,并希望使用我创建的宏粘贴它

我有以下代码:

Range("A2:W5000").Select
Selection.ClearContents
Range("A2").Select
ActiveSheet.Paste
With Selection.Interior
    .PatternColorIndex = 7
    .ThemeColor = xlThemeColorAccent2
    .TintAndShade = 0.799981688894314
    .PatternTintAndShade = 0
End With
Range("A2").Select
此宏在我的计算机上运行正常,但由于某些原因,在另一台PC上运行完全相同的宏时,
ActiveSheet.Paste出现错误

你知道为什么会这样吗


提前感谢您的建议。

您很可能正在使用受保护的工作表。因此,您将得到
1004
错误。在对工作表执行任何操作之前,请尝试检查工作表是否受到保护:

Sub TestMe()
    If ActiveSheet.ProtectContents Then
        MsgBox ActiveSheet.Name & " is protected!"
    Else
        Range("A2:W5000").Select
        Selection.ClearContents
        Range("A2").Select
        ActiveSheet.Paste
        With Selection.Interior
            .PatternColorIndex = 7
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
        Range("A2").Select
    End If
End Sub

我认为佩赫的答案是正确的。但我更新了你的代码以反映

Dim ws as Worksheet

set ws = ActiveSheet 'Setting the worksheet object and then referencing it for each Range will ensure that the macro doesn't get confused as to which sheet it should be getting the Range from.
ws.Range("A2:W5000").ClearContents 'No need to select cells first before clearing them
ws.Range("A2").PasteSpecial 'Once again, no need to select before pasting. It will do a normal paste if you do PasteSpecial only, but if you wanted to say paste values only it would look like this .PasteSpecial(xlPasteValues) 

With ws.Range("A2").Interior
    .PatternColorIndex = 7
    .ThemeColor = xlThemeColorAccent2
    .TintAndShade = 0.799981688894314
    .PatternTintAndShade = 0
End With
ws.Range("A2").Select 'No necessary unless you think that A2 won't be visible when the other user uses this macro. No harm in leaving it in though.
希望这能帮助你。祝你好运
Jason

问题在于,在运行宏之前,您必须开始复制。但是如果在宏中使用
.ClearContents
,则复制选择将丢失

因此,
之后的
.PasteSpecial
。ClearContent
无法工作

您可以使用

Sub test()
    Range("A1").Copy
    Debug.Print Application.CutCopyMode '=1 means something is copied
    Range("A2").ClearContents           'kills cutcopymode
    Debug.Print Application.CutCopyMode '=0 means nothing is copied
    Range("A3").PasteSpecial            'fails because nothing is selected for copy anymore
End Sub

所以解决办法是

  • 不要使用
    .ClearContents
    或在
    .Paste
    之前终止复制选择的任何其他操作
  • 写一个程序来…
  • .ClearContents
    首先,然后
  • 复制所需范围(如选择)并最终
  • 粘贴

其他PC上发生了什么?错误是什么?首先,您应该避免使用select:。并为每个范围指定一个工作表名称,如
工作表(“SheetName”)。范围(…)
,很可能问题已经解决。此外,我没有看到任何
Copy
操作,因此这可能也会导致错误。你到底想做什么。您不复制任何内容,只需清除一堆可能会取消任何现有复制操作的单元格。然后从原始选择中选择左上角的单元格并尝试粘贴。由于那里的操作暂停,无法联系到其他人。嗨,谢谢你的回复。我在另一张工作表上使用ctrl+c命令,并希望使用宏跳过它,宏在我的电脑上运行良好,但在另一张工作表上不起作用。抱歉,在问题中忘了提到这一点。实际问题是,
.ClearContents
杀死了
CutCopyMode
,因此不再选择任何内容进行复制,正如我在中所指出的。我的笔记本电脑上的一切都正常。
.ClearContents
终止
CutCopyMode
是正确的,但我认为只有在复制的内容位于同一工作簿中时才会这样做。我尝试过这个解决方案,但在另一台电脑上出现了相同的运行时错误。@SergeInácio当然你也会遇到相同的运行时错误
.ClearContents
已知会终止
CutCopyMode
,即使复制的内容不在同一工作簿中。如果你复制整个单元格,那就行不通了。仅当您在单元格内复制的内容时,此操作才有效,因为此时将使用Windows剪贴板而不是MSO剪贴板
.ClearContents
只会杀死MSO剪贴板,而不会杀死Windows剪贴板。谢谢@Pᴇʜ,我理解这一点,如果它不在我的笔记本电脑上工作,我会更好地理解它。问题是这个宏在另一台笔记本电脑上不工作,我觉得很奇怪。在我的笔记本电脑上,如果我从另一个工作簿复制单元格并运行宏,所有内容都会按预期粘贴,当我返回到另一个工作簿时,单元格仍会被复制。@SergeInácio我可以想象(没有测试),您的另一个工作簿可能不在同一个Excel实例中?然而奇怪的是,它可以在你的笔记本电脑上工作(根本不应该)。你不能相信它。因此,cutcopymode由
重置。ClearContents
是正常行为。您应该在
.ClearContents
之后使用copy,或者省略
.ClearContents