嗨,我正在尝试将Workbook1中一个工作表(“ HR&Finance”)中的所有注释复制到Workbook2中另一个工作表(“预算”)中。
到目前为止,我在工作簿1的工作表中记录了以下代码,并在打开两个工作簿的情况下运行了该代码,但它说下标超出范围错误...
您能给我一些如何解决此问题的建议吗?
谢谢!
Sub comment()
'
' comment Macro
'
'
Range(A:Z).Select
Selection.Copy
Windows("Workbook2").Activate
Range(A:Z).Select
ActiveSheet.Paste
End Sub
答案 0 :(得分:2)
Sub CopyComments()
Dim W As Workbook, W1 As Workbook, Sh As Worksheet, Sh1 As Worksheet
Set W = Workbooks("Workbook1.xlsx")
Set W1 = Workbooks("Workbook2.xlsx")
Set Sh = W.Worksheets("HR&Finance")
Set Sh1 = W1.Worksheets("Budget")
Sh.UsedRange.Copy
W1.Activate: Sh1.Activate
Sh1.Range("A1").PasteSpecial Paste:=xlPasteComments, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
但是,粘贴特殊注释的页面的结构必须与复制注释的页面的结构相同...
答案 1 :(得分:1)
我使用了与您的代码相同的区域,并将注释粘贴到book2单元格中。注释将与第一个工作簿出现在同一位置。
如果您想要评论列表,那么我们将不得不做其他事情
假定代码将在第一个工作簿中。
Sub CopyComments()
Dim bk1 As Workbook, bk2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Set bk1 = ThisWorkbook
Set bk2 = Workbooks("Book2.xlsx") ' change name of workbook
Set sh1 = bk1.Sheets("HR&Finance")
Set sh2 = bk2.Sheets("Budget")
With sh1
.Range("A:Z").SpecialCells(xlCellTypeConstants, 23).Copy
sh2.Range("A1").PasteSpecial xlPasteComments
End With
Application.CutCopyMode = False
End Sub