簡體   English   中英

VBA Excel:如何復制帶有圖像但沒有按鈕的工作表

[英]vba excel: how to copy worksheet with images but without buttons

我有一個帶有圖像和2個按鈕的工作表。 (發票)我想將帶有圖像但沒有2個按鈕的工作表復制到新工作簿的新表中,我想用vba宏來做。 目前,我使用通常的復制命令執行此操作,並為2個按鈕使用了額外的刪除命令。

我敢肯定有一種更簡單的方法可以做到這一點。 我嘗試了這個

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

這些會松開按鈕,但圖像也消失了。 但我想保持形象。

我希望你們能幫助我。

提前致謝。

解釋在下面的代碼注釋中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM