2017-03-13 45 views
0

我有一個工作表從「workbook.protected」複製到新的問題。宏執行如下操作:首先取消保護所需工作表,然後創建新工作簿,將工作表複製到新工作簿,將新工作簿中的所有單元格重新保存爲.values,並保存&關閉工作簿,最後保護父工作簿。 問題在於,由於某些原因,新工作簿不包含值,而是對父工作簿的引用。你能否就這個問題徵求意見? 我使用的代碼如下:VBA Copy Sheet.PROTECTED到新的工作簿ISSUE

Global path As String 
Global filename As String 

Sub SaveSheet() 
Application.ScreenUpdating = False 
Dim sh1 As Worksheet 
Dim sh2 As Worksheet 
On Error Resume Next 
Set sh1 = ThisWorkbook.Worksheets("sheet1") 
Set sh2 = ThisWorkbook.Worksheets("sheet2") 
sh1.Unprotect Password:="password" 
sh2.Unprotect Password:="password" 
Dim a As String 

path = "\\path\" 
filename = "file1234" 

Set wb = Workbooks.Add 

ThisWorkbook.Activate 
Sheets("sheet1").Copy Before:=wb.Sheets(1) 
ThisWorkbook.Activate 
Sheets("sheet2").Copy Before:=wb.Sheets(1) 


With wb 
Application.DisplayAlerts = False 
wb.Activate 
Sheets(5).Delete 
Sheets(4).Delete 
Sheets(3).Delete 
Sheets("sheet1").Select 
Range("A1:N1000") = Range("A1:N1000").Value 
Sheets("sheet2").Select 
Range("A1:BW1000") = Range("A1:BW1000").Value 


Application.DisplayAlerts = True 
End With 

ChDir path 

With wb 
If Len(Dir(path, vbDirectory)) = 0 Then 
MkDir path 
.SaveAs path & filename & ".xlsb", FileFormat:=50 
Else 
.SaveAs path & filename & ".xlsb", FileFormat:=50 
End If 
.Save 
.Close 
End With 
    With sh1 
     .Cells.Locked = False 
     .Cells.SpecialCells(xlCellTypeFormulas).Locked = True 
     .Protect Password:="password" 
    End With 
    With sh2 
     .Cells.Locked = False 
     .Cells.SpecialCells(xlCellTypeFormulas).Locked = True 
     .Protect Password:="password" 
    End With 
End Sub 
+1

哪來的 「.....代碼」 :) –

+0

這:) Nathan_Sav – Lincoln

+0

範圍( 「A1:N1000」)=範圍( 「A1:N1000」)的價值 - 這是一個非常有趣的。公式的消失方式。它工作嗎?在其他消息中,「Select」和「Activate」可能有所不同。 – Winterknell

回答

0

此過程使用數組來保存源工作表,名稱爲&的密碼。它着重於解除工作表的保護,添加新的工作簿並複製目標工作表(值和格式),只需添加其他部分(即保護,保存等),這些部分在您的代碼中似乎沒問題。

'Have these declaration at begining of the module 
Option Explicit 
Option Base 1 

Sub Wsh_CopyTo_NewWbk() 
Dim aWsh As Variant 
aWsh = [{"Sheet1","Wsh1";"Sheet2","Wsh2"}] 
Dim aWshSrc(2) As Worksheet 
Dim wbk As Workbook, wsh As Worksheet 
Dim vItm As Variant, b As Byte 

    Rem Set Worksheet Array 
    With ThisWorkbook 
     For b = 1 To UBound(aWsh) 
      .Worksheets(aWsh(b, 1)).Unprotect Password:=aWsh(b, 2) 
      Set aWshSrc(b) = .Worksheets(aWsh(b, 1)) 
    Next: End With 

    Rem Add New Workbook 
    Set wbk = Workbooks.Add 
    With wbk 
     Rem Delete All Worksheets but One 
     Application.DisplayAlerts = False 
     For Each wsh In .Worksheets 
      With wsh 
       If .Index = 1 Then .Name = "!DELETE" Else .Delete 
     End With: Next 
     Application.DisplayAlerts = True 

     Rem Copy Worksheets 
     For Each vItm In aWshSrc 
      vItm.Copy After:=Sheets(.Sheets.Count) 
      Set wsh = .Sheets(.Sheets.Count) 
      wsh.UsedRange.Value = wsh.UsedRange.Value2 
     Next 

     Rem Delete Reamining Worksheet 
     Application.DisplayAlerts = False 
     .Worksheets("!DELETE").Delete 
     Application.DisplayAlerts = True 

    End With 
End Sub 
0

使用Copy不指定參數時,將紙張複製到剪貼板,然後Pastespecial

像這樣的事情

ThisWorkbook.Activate 
Sheets("sheet1").Copy 
wb.Sheets(1).PasteSpecial 

PasteSpecial需要一個參數Link,默認爲false。所以不需要指定它。如果它是錯誤的,它不應該保留對原始工作表的任何引用

+0

由於某些原因,第一張紙張複印OK。但是,當下一個複製的Excel再次創建一個新的工作簿...不知道爲什麼 – Lincoln