首页 > 解决方案 > 复制一系列单元格并选择一个新单元格后,PasteSpecial 失败

问题描述

我试图解决这个看似简单的问题,但找不到解决方案。我正在尝试复制一个选定的单元格,以及后面的 2 个单元格,然后创建一个新行并将内容粘贴到新行的第一个单元格中。

第一部分工作正常,选择后提示询问这是否是要复制的正确单元格,然后进行复制,并使用选定的第一个单元格创建新行,但代码在 PasteSpecial 上失败。

你会在代码中看到我尝试过的那些没有用的注释。

我将不胜感激所提供的任何帮助。提前致谢。戴夫

Sub SelectCell_on_RowThenAdd2andAskYorN_AddRow_Paste()

Dim Msg, Style, Response, MyString
    Msg = "Is Job Number the job you wish to copy?"
    Style = vbYesNo
    Response = MsgBox(Msg, Style)
    
    If Response = vbYes Then   ' User chose Yes.
        MyString = "Yes"   ' Perform some action.

    ActiveCell.Select 'Takes active cell and offsets 1 to the right
    Selection.Resize(Selection.Rows.Count, _
        Selection.Columns.Count + 2).Select ' Resizes selection by 2 rows
        Selection.Copy
        
    Else   ' User chose No.
        MyString = "No"   ' Perform some action.
    
    MsgBox "Choose the correct Job Number," & (Chr(13)) & "then Click the Copy Button.", vbOKOnly
    
    End If
    
    Dim oNewRow As ListRow
    Set oNewRow = Selection.ListObject.ListRows.Add(AlwaysInsert:=True)
    oNewRow.Range.Cells(1, 1).Select
    'Cells(Selection.Row, 1).Select  --- Did not work
    'ActiveSheet.Paste  --- Did not work
    'Paste Special gives error PasteSpecial Method of Range Class Failed
    Selection.PasteSpecial Paste:=xlValues


End Sub

标签: excelvba

解决方案


推荐阅读