excel - 复制一系列单元格并选择一个新单元格后,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
解决方案
推荐阅读
- laravel - 使用 Laravel 5.8 在后台保存外部 API 数据的最佳实践
- ansible - junos_l2_interface 删除描述,不是吗?
- javascript - 渲染对象 React 数组
- android-toolbar - 在 androidx.appcompat.app.ActionBarDrawerToggle 中不显示汉堡包按钮
- typescript - 具有接口类型之一的变量?
- qt - 由于 ssl 问题,无法在 Ubuntu 上安装 QT4
- reactjs - 动态组件在 React 中不起作用
- javascript - 为什么传递函数 ref 而不是调用函数的 lambda 会有所不同?
- c# - 过滤同一 ObservableCollection 的多个 CollectionView - WPF
- java - 按对象中的不同参数对 ArrayList 进行排序