我有一个工作表,当用户点击按钮时,该工作表被复制并粘贴到新的工作表中。我设法复制工作表中的所有内容(形状,按钮等),但包含使用命名范围的下拉列表的组合框除外(使用以下代码创建的下拉列表:http://www.contextures.com/xlDataVal11.html)。
我尝试为此录制一个宏并获得以下(简化)
ActiveSheet.Shapes.Range(Array("ExampleCombo")).Select
Selection.Copy
ActiveSheet.Paste
有了这个,我明白组合框被视为一个形状。为了复制所有组合框并将它们放在新工作表中的正确位置,我尝试了以下内容:
Sub CopyCombos ()
Dim ws_new As Worksheet
Dim ws_old As Worksheet
Dim Special_Shape As Shape
Dim Special_Shape_COPY As Shape
Dim Position_Left As Single
Dim Position_Top As Single
Dim Position_Width As Single
Dim Position_Height As Single
Set ws_old = ActiveSheet
ActiveWorkbook.Worksheets.Add
Set ws_new = ActiveSheet
For Each Special_Shape In ws_old.Shapes
'Copy position
Position_Left = Special_Shape.Left
Position_Top = Special_Shape.Top
Position_Width = Special_Shape.Width
Position_Height = Special_Shape.Height
'Copy
Special_Shape.Copy
'Paste
ws_new.Paste '<=== Here's the problem! But why?
'Rename
Set Special_Shape_COPY = Selection
'Put in right place
Special_Shape_COPY.Left = Position_Left
Special_Shape_COPY.Top = Position_Top
Special_Shape_COPY.Width = Position_Width
Special_Shape_COPY.Height = Position_Height
Next Special_Shape
End Sub
我收到一条错误消息&#34;此时无法进入休息模式&#34;直接粘贴新工作表中的组合框。我怎么解决这个问题?
答案 0 :(得分:0)
而不是:
Set ws_old = ActiveSheet
ActiveWorkbook.Worksheets.Add
Set ws_new = ActiveSheet
并复制,使用:
ActiveSheet.Copy After:=Sheets(Sheets.Count)
这将产生完整的副本:
答案 1 :(得分:0)
Gary的学生复制工作表的答案在同一工作簿中复制时效果很好:
ActiveSheet.Copy After:=Sheets(Sheets.Count)
但是,由于格式颜色不同,因此将工作表复制到另一个(新)工作簿时会出现问题。为了解决这个问题,我在添加新工作簿之前使用了以下代码:
Workbooks.Add Template:="Workbook"