2017-05-10 3 views
0

だから私はこのコードが完成したと思っていましたが、シート1から他のシートに移動してからシート1に戻ると、msgboxがポップアップし、保護されていないことを通知しますシート。まだマクロが正常に動作していても、「保護されたシートにこれを行うことはできませんか?

私は、コードが想定していたとおりに正しく動作しているため、なぜこのようなことが起こっているのか不明です。どんな助けもありがとうございます。

編集:私はおそらくシートがパスワード "1"で保護されていると述べたはずです。私はこれが最も適切なパスワードではないことを認識しています。私はこの問題を解決するために、より簡単にアクセスできます。

Sub freezesheet() 
'set variable for the naming of the new sheet 
Dim newname As String 

'assignes our open variable to a designated value 
Sheets("Sheet1").Activate 
newname = Sheets("Sheet1").Range("C2").Value 

'copies the sheet to a new sheet after the designated tab 
Sheets("Sheet1").Copy after:=Sheets(3) 
ActiveSheet.Name = newname 

'unprotects the sheet so we can copy and paste as values 
ActiveSheet.Unprotect "1" 

'makes all of the formulas on the sheets into values and returns you to the original sheet 
Cells.Select 
    selection.Copy 
    selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Application.CutCopyMode = False 

'Re-protects sheet to ensure that we don't make changes to historical data. 
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _ 
     , AllowFormattingCells:=True, AllowFormattingColumns:=True, _ 
     AllowFormattingRows:=True 

Sheets("Sheet1").Activate 

End Sub 
+0

これを2回実行すると、シート名がすでに取得されている場合、シートの名前を「newname」(C2の値)に変更しようとしますか? – Wolfie

+0

良い点、これは既に私がこのツールで扱ってきた問題です。セルC2の値は、マクロを実行する各インスタンスの前にユーザーによって変更されます。 – DataNinja

+0

私は以下の回答を掲載しましたが、あなたの問題点が明白ではありません。これはシート1に戻るときに起こると言っています。このサブはシートチェンジイベントによってトリガーされていますか? – Wolfie

答えて

1

下記の再書き込みでは、いくつかの問題が解決されます。うまくいけばそれらの世話をして、サブは間違いなく実行する必要があります。すなわち:

  • 同じ名前の既存のシートをチェックし
  • 潜在的な衝突を回避するためには、クリップボード
  • に大量のデータを置くことを避けるために.Valueを使用し
  • 必要はありませんが、完全な範囲を認定する際の選択と有効化回避ThisWorkbook

を使用すると、詳細についてはコメントを参照してください

Sub freezesheet() 
    'set variable for the naming of the new sheet 
    Dim newname As String 
    'assigns newname variable to a designated value 
    newname = ThisWorkbook.Sheets("Sheet1").Range("C2").Value 
    ' Check if sheet name already exists 
    Dim sh as worksheet 
    On Error Resume Next 
    Set sh = ThisWorkbook.Sheets(newname) 
    On Error GoTo 0 
    If Not sh Is Nothing Then 
     MsgBox "Error: sheet name already exists, aborted" 
     Exit Sub 
    End If 
    'copies the sheet to a new sheet after sheet 3 
    ThisWorkbook.Sheets("Sheet1").Copy after:=Sheets(3) 
    With ThisWorkbook.Sheets(4) 
     .Name = newname ' New sheet was after sheet 3, so now sheet 4 
     'unprotects the sheet so we can copy and paste as values 
     .Unprotect "1" 
     'makes all of the formulas on the sheets into values 
     .UsedRange.Value = .UsedRange.Value 
     'Re-protects sheet to ensure that we don't make changes to historical data. 
     .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _ 
       , AllowFormattingCells:=True, AllowFormattingColumns:=True, _ 
       AllowFormattingRows:=True 
    End With 
    ThisWorkbook.Sheets("Sheet1").Activate 
End Sub 
+0

すべきでない場合は、何もしないでください。 には何らかの標識が含まれていますか? shに何も書かれていない場合は、同じ名前のワークシートがないので、コードは正しく処理されるはずです。もしshが何もないなら、それは終わるべきですか? – DataNinja

+1

はい@DataNinja、そうです!私の過ちは、私は上記のコードをテストしませんでした。編集の間違いを修正しました。 – Wolfie

+0

私は確認することができます、これは完璧に動作します。ご協力ありがとうございました。大変感謝しています。 – DataNinja

関連する問題