保護されているシートでマクロ実行するとエラー

このQ&Aのポイント
  • Excel2010で勤務表を作っている際に、保護されている状態のシートでマクロを実行すると「実行時エラー'1004'アプリケーション定義またはオブジェクトの定義エラーです」というエラーが発生します。
  • 具体的には、セルの網掛けを変更するマクロを実行するとエラーが発生します。保護されていない行に実行しているにもかかわらず、エラーが発生しています。
  • 保護されている行ではなく、まだ保護されていない行に対して実行しているため、エラーが発生している可能性があります。
回答を見る
  • ベストアンサー

保護されているシートでマクロ実行するとエラー

Excel2010で勤務表を作っています。 A列にとある文字列(承認)と入力すると、その行が保護されるマクロを、下記URLからコピペして使わせて頂いてます。 http://questionbox.jp.msn.com/qa3277541.html 勤務表なので、土日祝日は網掛けになるよう条件付き書式を使っています。 休暇取得した場合は網掛けを付けて、休日出勤した場合には網掛けなしにしたり出来るようマクロをマクロの自動記録で作りました。 ところが、どこかの行が保護されている状態で、セルの網掛けを変更するマクロを実行すると 「実行時エラー'1004'アプリケーション定義またはオブジェクトの定義エラーです」と出てしまいます。 保護されている行ではなく、まだ保護はされていない行に実行しています。 エラーが出ているのは網掛けマクロから出ています。 以下、今エクセルファイルにあるマクロの構文になります。 網掛けマクロは全部で4つ作りました。 1)休日出勤した際に条件付き書式をクリアして網掛けなしにするマクロです。 Sub 休日出勤() ' 条件書式クリア Selection.FormatConditions.Delete End Sub ※エラーになっている部分です。 2)平日休んだ日に網掛けをするマクロです。 Sub 休日() ' 網掛け With Selection.Interior .ColorIndex = 0 .Pattern = xlGray16 .PatternColorIndex = xlAutomatic End With End Sub 3)2)のマクロで休日にしたけど、やっぱり出勤したという時に、1)だと網掛けなしにならなかったので、網掛けなしにするマクロを作りました。 Sub網掛けなし() ' 網掛けなし With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 4)ごちゃごちゃいじってしまって最初の状態に戻したいと思ったので条件付き書式を再設定するマクロを作りました。が、2)の休日マクロを実行したセルは元に戻らないので仕方なく3)の網掛けなしマクロを実行しなければなりません。 Sub 書式クリア() ' 条件書式再設定 Range("A6:K36").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=OR(WEEKDAY($B6)=1,COUNTIF(祝日,$B6))" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False End Sub ※この中のSelection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6"の部分がエラーと出ています。 ※1)から3)は網掛けしたいところ、網掛けなしにしたいところを範囲選択してから実行しなければなりません。 その他、保護を解除する際にパスワード認証が欲しかったので、「保護解除」ボタンを押すためにパスワード認証させるマクロもあります。 これは特に問題なく動いています。 5)パスワード認証つき保護解除マクロ Sub password() Dim pw As Long pw = Application.InputBox( _ prompt:="パスワード入力", Type:=1) If pw <> "123" Then MsgBox "パスワードが違います" Exit Sub Else MsgBox "保護解除しました" ActiveSheet.Unprotect End If End Sub 6)行ごとに保護するマクロです。 Private Sub Worksheet_Change(ByVal Target As Range) Dim r, rng As Range Set rng = Intersect(Target, Columns(1)) If Not rng Is Nothing Then If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect End If For Each r In rng If r.Value = "承認" Then r.EntireRow.Locked = True Else r.EntireRow.Locked = False End If Next r ActiveSheet.Protect DrawingObjects:=True, Contents:=True End If End Sub マクロに関してはド素人で、自動記録かWebで調べて見つけたマクロをちょっと加工して使う程度です。 どうか知恵をお貸しください。よろしくお願いします。

  • wyoco
  • お礼率75% (3/4)

質問者が選んだベストアンサー

  • ベストアンサー
  • kuwa_98
  • ベストアンサー率66% (79/119)
回答No.1

シートをプロテクトするとき、書式の変更を可能として下さい。 プロテクトする行を下記に書き換えて下さい。 ActiveSheet.Protect DrawingObjects:=True, Contents:=True_ , AllowFormattingCells:=True

wyoco
質問者

お礼

ご回答ありがとうございました。 書き換えたところ無事に網掛けマクロが動きました! 本当にどうもありがとうございました。

関連するQ&A

  • Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教

    Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教示お願いします。 Excel2007使用。下記はマクロを記録として作成しました。省略できるものは省略して見やすくしたいのですが、どこを修正していいのか困っています。 範囲は「A2:A1000、B2:B1000」としていますが、「値が入っている範囲」としたい場合はどのように記述したらよいのでしょうか。 マクロ現在勉強中です。よろしくお願いいたします。 Sub CHK() Range("A2:A1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($B$2:$B$1000,A2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False Range("B2:B1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($A$2:$A$1000,B2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • EXCELでマクロの記録を繰り返して

    EXCELでマクロの記録を繰り返して重複した行は分かる範囲でまとめたのですが、もう少しまとめられないかと思っています。 withで、まとめられなくても、順番を入れ替えたらまとまるものとか、逆にまとめようが無いものとか教えて頂けたらと思います。 よろしくお願いします。 With Selection .Phonetics.Visible = False 'フリガナ表示をオフに .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($L3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .Pattern = xlSolid .PatternColorIndex = 0 .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),NOT(ISBLANK($D3)))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .TintAndShade = 0 .PatternTintAndShade = 0 .Color = 5296274 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($R3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent5 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False

  • 保護されているシートでマクロ実行するとエラー

    Excel2010で勤務表を作っています。 A列にとある文字列(承認)と入力すると、その行が保護されるマクロを、下記URLからコピペして使わせて頂いてます。 http://questionbox.jp.msn.com/qa3277541.html 勤務表なので、土日祝日は網掛けになるよう条件付き書式を使っています。 休暇取得した場合は網掛けを付けて、休日出勤した場合には網掛けなしにしたり出来るようマクロをマクロの自動記録で作りました。 ところが、どこかの行が保護されている状態で、セルの網掛けを変更するマクロを実行すると 「実行時エラー'1004'アプリケーション定義またはオブジェクトの定義エラーです」と出てしまいます。 保護されている行ではなく、まだ保護はされていない行に実行しています。 そこで一旦保護の解除をしてから、セルの網掛けを変更するマクロを実行してみるとちゃんと動きます。 保護をかける時に、網掛け(セルの書式設定)を許可すれば動くのかな?とか思ったのですが、どこにどういう構文を入れたらいいのか、はたまたその認識すら間違っているのかも分かりません。 マクロに関してはド素人で、自動記録かWebで調べて見つけたマクロをちょっと加工して使う程度です。 どうか知恵をお貸しください。よろしくお願いします。

  • エクセルのマクロの実行場所(セル)の指定(初心者)

    こんにちは。いつもお世話になっております。 ここで色々教わりながら、本も参考に色々試行錯誤しながらあがいている超初心者です。 下記のマクロを個人ブックに保管して、複数あるブックのセルに書式設定をしようとしています。 超初心者故マクロのコードが書けないので、マクロの記録で下記の動作をマクロにしようとしました。 文書Noと入力した日付の整合性をチェックし、外れた場合(例:2017年4月なら、Noは1704からら始まらないと)青斜体にしたい。 一応正常に動くことは確認したのですが、この書式設定を実行するセルがブックによってちがっています。 下記の例は入力日をA1に入れることにしていますが、実際には列も行も違っています。(さらにはこれで設定した書式を、書式のコピーで下に必要な行数分コピーすることになります)  「相対参照で記録」とか、できたマクロのコードの指定セルの$を取ってみるとか思いつくことは試行してみたのですが、うまく行きません。 選択しているセルに設定する為には、何処をどう修正すれば良いのでしょう? 対象ブックが500、使用頻度が高く当面書式設定したいブックが200近くあるので何とかマクロで対処したく宜しくお願いします。 Sub 文書Noと日付() ' Keyboard Shortcut: Ctrl+d Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=LEFT($A$1,4)<>TEXT(TODAY(),""yymm"")" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Bold = False .Italic = True .Color = -65536 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点

    エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点以上のセルの背景を赤にする、というマクロを作成したいと思います。選択する範囲は毎回異なります。以下のマクロはマクロの記録で作成したものです。このRangeの部分をマウスで選択した範囲に変更したいのですが、どうしたら良いのでしょうか?範囲の取得はマクロを実行して、途中で「マウスで範囲を指定してください」というメッセージを表示させてからにしたいのですが。 Sub 背景を赤() Range("B13:E19").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=80" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With End Sub

  • エクセルのマクロ、VBAに関する質問

    エクセルのマクロ、VBAに関する質問です。 下記コード3行目の"ここヘルプ!"のところを ”「選択範囲左上のセル Selection(1)」を「A1形式」で「行だけ絶対参照 xlAbsRowRelColumn」したものに10足した値” にしたいです。 例:選択範囲がA1:D5なら”A$1+10”   選択範囲がB4:H78なら”B$4+10” もうちょっとで出来る気がするので教えて下さい。 Sub 基準値+1o以上をハッチング() Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="ここヘルプ!" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 52479 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • 複数の条件付書式

    B列に1~5までに数字がランダムにはいっています。 条件は ・B列が空白ならA列は無色 ・B列が1ならA列は赤 ・B列が2ならA列は青 ・B列が3ならA列は黄色 ・B列が4ならA列は緑 というのをマクロで行ないたいです。 自分で記録して Sub Macro32() Columns("A:A").Select Selection.FormatConditions.Delete Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=""""" Selection.FormatConditions(1).Interior.Pattern = xlNone Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=1" Selection.FormatConditions(2).Interior.ColorIndex = 38 Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=2" Selection.FormatConditions(3).Interior.ColorIndex = 40 End Sub 4つ目に Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1=3" Selection.FormatConditions(4).Interior.ColorIndex = 32 とするとエラーになります。 3つ以上の条件付書式の書き方を教えてください。 あとこの構文のSelectionはwithで省略できますか? よろしくお願いします。

  • ワークシートのセルの書式設定の罫線をマクロでひく。

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (1)のところでBORDERクラスのlinestyle プロパティを設定できません。がでる対策をおしえてください。 Sub Macro1() ' Dim d As Long Sheets("abc").Select '罫線を引く d = Range("A65536").End(xlUp).Row Range("A1", Cells(d, 1)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous  ‘(1) .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End Sub

  • Excel VBAで罫線を引くマクロを書きたい

    Excel VBAで罫線を引くマクロを書きたいと思っています。 で、文末のコードを書きました。(というかマクロ記録したものほぼそのもの) これだとある程度動くのですが、内側線が無いような範囲を選択した場合にはエラーになってしまいます。 内側の線を引く際にIF文をかまさなければならないように思うのですが、イマイチわかりません。 この点について教えてください。 また、コードが冗長であるようにも思えます。もう少しスマートな書き方があればあわせて教えてください。 よろしくお願いします。 Sub 枠線基本() ' 周囲 With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With ' 内側 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With End Sub

  • シート保護をすると実行エラーになります。

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Intersect(Target, Range("A1:A2000")) Is Nothing Then Exit Sub With Selection.Interior If .ColorIndex = xlNone Then .ColorIndex = 4 Else .ColorIndex = xlNone End If End With Cancel = True End Sub A列任意のセルをダブルクリックすると色が変わるコードを組んでいます。しかしながら、 A列のみロックを解除したのち、シート保護をすると、上記の実行がエラーになります。 どのようにすればエラーを回避できるのかお知恵をかしていただければ幸いです。

専門家に質問してみよう