Excel运行时错误复制和粘贴

时间:2016-10-17 19:46:23

标签: excel excel-vba vba

我创建了一本工作书,模仿游戏yahtzee的项目。通过一个宏我做了它,你可以按一个按钮来复制和粘贴一组类似于骰子面部的9个单元格。一切似乎都运行良好,但随机我会得到一个运行时错误1004.我没有编写代码只是因为我还没有达到那个级别。我有多个不同的按钮,它们执行相同的复制和粘贴,但它并不总是同一个给我错误的按钮。这是它的副本。我真的很感激任何类型的帮助。

Sub Hold4()
'
' Hold4 Macro
'

'
    Selection.Copy
    Range("N10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    With Selection.Font
        .Name = "Wingdings 2"
        .Size = 28
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Range("N2:P4").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
End Sub

以下是错误中突出显示的部分......

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

1 个答案:

答案 0 :(得分:0)

尝试使用avoiding the use of .Select并使用变量:

Sub Hold4()
Dim myRng   As Range
Set myRng = Selection
myRng.Copy
Range("N10").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
With myRng.Font
    .Name = "Wingdings 2"
    .Size = 28
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
End With

With Range("N2:P4")
    With .Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With .Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
End With
End Sub