2016-09-22 96 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

你的意思是太重了麼? –

相關問題