2016-09-22 100 views
0

我有一个带有图像和2个按钮的工作表。 (发票) 我想复制工作表与图像,但没有2按钮到一个新的工作簿的新工作表,我想用vba宏做。 在我用普通的复制命令执行它的时刻,并为2个按钮使用额外的删除命令。vba excel:如何复制带有图像但没有按钮的工作表

我相信有一个更简单的方法来做到这一点。 我想这...

Application.CopyObjectsWithCells = False 
Sheets("invoice").Select 
Sheets("invoice").Move After:=Workbooks("invoices").Sheets(1) 
Application.CopyObjectsWithCells = True 

这些失去的按钮,但图像也消失了。但我想保留图像。

我希望你们能帮助我。

在此先感谢。

回答

0

的解释是在下面的代码中的注释:

Option Explicit 

Sub CopySheet_WO_Btn() 

Dim newWB      As Workbook 
Dim ShtOrig      As Worksheet 

Dim Obj       As OLEObject 
Dim PicShape     As Shape 
Dim PicLeft      As Double 
Dim PicTop      As Double 

Set ShtOrig = ThisWorkbook.Sheets("invoice") 

' loop through all objects in "invoice" sheet 
For Each Obj In ShtOrig.OLEObjects 

    ' if OLE Object is type CommanButton make it Un-Visible (not to copy with the sheet) 
    If Obj.progID = "Forms.CommandButton.1" Then 
     Obj.Visible = False 
    End If 

Next Obj 

Set newWB = Workbooks.Add(xlWBATWorksheet) 
ShtOrig.Copy newWB.Sheets(1) 

' loop through all shapes in "invoice" sheet and copy the pictures 
For Each PicShape In ShtOrig.Shapes 
    If PicShape.Name = "Picture 1" Then 
     PicLeft = PicShape.Left 
     PicTop = PicShape.Top 
     PicShape.Copy 

     newWB.Sheets(1).Paste 
     Selection.ShapeRange.Left = PicLeft 
     Selection.ShapeRange.Top = PicTop 
    End If 
Next PicShape 

' loop again and return the CommandButtons to be Visible in "invoice" sheet 
For Each Obj In ShtOrig.OLEObjects 
    Obj.Visible = True 
Next Obj 

End Sub 
+0

非常感谢您的帮助!这对我的小发票有点太大了,但是你给了我一个好主意。我发现你的帮助如何使按钮不可见,然后将工作表复制到新工作簿中,然后再次使原始发票中的按钮可见!非常感谢 –

+0

你的意思是太重了么? –

相关问题