VBAエラーの対処方法を教えてください

このQ&Aのポイント
  • VBAを実行する際に、MX.Borders(xlDiagonalUp).LineStyle = xlContinuousの部分でエラーが発生します。エラーの対処方法について教えてください。
  • VBAの実行中にMX.Borders(xlDiagonalUp).LineStyle = xlContinuousの部分でエラーが出ています。どのように修正すればいいですか?
  • VBAのプログラムを実行すると、MX.Borders(xlDiagonalUp).LineStyle = xlContinuousの部分でエラーが発生します。エラーメッセージを解決する方法を教えてください。
回答を見る
  • ベストアンサー

VBA  エラー

Sub 上下カット1() Dim MyR As Range, MyMax As Integer, MyMin As Integer Dim MX As Range, MN As Range With ActiveSheet MyMax = WorksheetFunction.Max(.Range("D15:P15")) MyMin = WorksheetFunction.Min(.Range("D15:P15")) For Each MyR In .Range("D15:P15") If MyR.Value = MyMax Then Set MX = MyR If MyR.Value = MyMin Then Set MN = MyR Next MX.Borders(xlDiagonalUp).LineStyle = xlContinuous MN.Borders(xlDiagonalUp).LineStyle = xlContinuous End With With ActiveSheet MyMax = WorksheetFunction.Max(.Range("D16:P16")) MyMin = WorksheetFunction.Min(.Range("D16:P16")) For Each MyR In .Range("D16:P16") If MyR.Value = MyMax Then Set MX = MyR If MyR.Value = MyMin Then Set MN = MyR Next MX.Borders(xlDiagonalUp).LineStyle = xlContinuous MN.Borders(xlDiagonalUp).LineStyle = xlContinuous End With End Sub を実行すると、 MX.Borders(xlDiagonalUp).LineStyle = xlContinuous の部分にエラーがでます。 対処方法を教えてください。

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

  • ベストアンサー
  • mt2008
  • ベストアンサー率52% (885/1701)
回答No.2

> 2.デバックで黄色く表示されます。 それはエラーではなく、ステップ実行でその行で処理を中断していると言うだけの状態です。 もしかすると過去に設定したブレイクポイントの情報が残っているのかもしれません。 #Excel2003の頃までは良くありました。 デバッグ→全てのブレークポイントの解除 を試してください。

nakeko
質問者

補足

この度は、大変申し訳ありませんでした。 いろいろとアドバイスいただきありがとうございます。試してみます。

その他の回答 (2)

  • kybo
  • ベストアンサー率53% (349/647)
回答No.3

D15:P15の範囲で最小値と最大値を求めるので、数値が入力されていなければなりません。 D15:P15の範囲に数字として入力されていますか? 全て文字列だった場合、エラーになります。

  • mt2008
  • ベストアンサー率52% (885/1701)
回答No.1

補足願います。 1. MX.Borders(xlDiagonalUp).LineStyle = xlContinuous は4か所ありますがどこで発生していますか? 2. どんなエラーですか? 3. セルの保護が掛っていませんか? 4. ちょっとだけ文面を変えて別IDで同じ質問をするのは何故ですか?

nakeko
質問者

補足

補足です。 1.一番上です。 2.デバックで黄色く表示されます。 3.保護はかかってないです。 4.最初に出たエラーから試行錯誤し、違う形でデバックが出たので質問し、IDが違うのはエラーも違う種類だと思ったのと違うサイト?だと思っていたからです。 大変申し訳ありませんでした。 よろしくお願いいたします。

関連するQ&A

  • VBA エラー

    Sub 上下カット1() Dim MyR As Range, MyMax As Integer, MyMin As Integer Dim MX As Range, MN As Range With ActiveSheet MyMax = WorksheetFunction.Max(.Range("D15:P15")) MyMin = WorksheetFunction.Min(.Range("D15:P15")) For Each MyR In .Range("D15:P15") If MyR.Value = MyMax Then Set MX = MyR If MyR.Value = MyMin Then Set MN = MyR Next MX.Borders(xlDiagonalUp).LineStyle = xlContinuous MN.Borders(xlDiagonalUp).LineStyle = xlContinuous End With With ActiveSheet MyMax = WorksheetFunction.Max(.Range("D16:P16")) MyMin = WorksheetFunction.Min(.Range("D16:P16")) For Each MyR In .Range("D16:P16") If MyR.Value = MyMax Then Set MX = MyR If MyR.Value = MyMin Then Set MN = MyR Next MX.Borders(xlDiagonalUp).LineStyle = xlContinuous MN.Borders(xlDiagonalUp).LineStyle = xlContinuous End With End Sub を実行すると”オブジェクト変数またはWithブロック変数が設定されていません。”と出ます。 どうしたらいいですか?

  • すべてのシートでマクロを実行したい

    以下のプログラムでは、選択したシートのみマクロが動作しています。ネット検索で見よう見まねで作ったため何がまちがっているのかわかりません。ご教示いただけるとありがたいです。 ・月の予定表で利用者が休みの日に斜線を引くマクロ ・入力ミスを防ぐためシート保護をしている Sub すべてのシート() Dim s As Worksheet For Each s In Worksheets s.Select Call 斜線 Next End Sub Sub 斜線() ActiveSheet.Unprotect Password:="1234" For i = 1 To Range("E10").End(xlDown).Row Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlNone If Range("E10").Value = 0 Then Exit Sub If Cells(i, "E").Value = "日" And Range("BP9").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "月" And Range("BP10").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "火" And Range("BP11").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "水" And Range("BP12").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "木" And Range("BP13").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "金" And Range("BP14").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "E").Value = "土" And Range("BP15").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If If Cells(i, "AY").Value = "祝日" And Range("BP16").Value = 0 Then Range("AS" & i).Borders(xlDiagonalUp).LineStyle = xlContinuous End If Next i ActiveSheet.Protect Password:="1234" End Sub

  • EXCEL マクロ 最小値と最高値

    今マクロを作成しております。 A1~D1の中の数字の最小値・最高値に罫線の斜め線を入れたいのですが、 例) A1  B1  C1  D1    10  15  15   8   下記のマクロだと上記のように同じ数字があるとその両方に斜め線が入ってしまいます。 斜め線を入れたいのは、最小値1つと最高値1つで、上記の場合B1とC1が最高値なので C1に斜め線を入れたいのです。 仮にA1とD1が同数で最小値または最高値の場合D1に斜め線    B1とD1が同数で最小値または最高値の場合D1に斜め線 (同数の場合より左側にあるセルに斜め線) どのようにしたらいいのかアドバイスいただけますか? Sub 上下カット() Dim MyR As Range Dim MyMax, MyMin With ActiveSheet MyMax = WorksheetFunction.Max(.Range("A1:D1")) MyMin = WorksheetFunction.Min(.Range("A1:D1")) For Each MyR In .Range("A1:D1") If MyR.Value = MyMax Or MyR.Value = MyMin Then MyR.Borders(xlDiagonalUp).LineStyle = xlContinuous End If Next End With End Sub

  • エクセルVBA 条件判断(斜線)

    エクセル VBAにて If MasterData(masteridx, 14).Borders(xlDiagonalUp).LineStyle.xlContinuous = Value Then wb.Sheets(Seisannippou & CStr(SHeetNumber)).Range("T12").Borders(xlDiagonalUp).LineStyle = xlContinuous Else wb.Sheets(Seisannippou & CStr(SHeetNumber)).Range("T12").Value = MasterData(masteridx, 14) wb.Sheets(Seisannippou & CStr(SHeetNumber)).Range("T12").Font.Bold = True wb.Sheets(Seisannippou & CStr(SHeetNumber)).Range("T12").Font.Size = 22 End If 以上のようにマスタシート対象品の選択セルが斜線だったら 生産日報シート対象品の選択セルに斜線を記入するようにしたいのですが、If MasterData(masteridx, 14).Borders(xlDiagonalUp).LineStyle.xlContinuous = Value Then wb.Sheets(Seisannippou & CStr(SHeetNumber)).Range("T12").Borders(xlDiagonalUp).LineStyle = xlContinuous の書き込み方(マスタシートの斜線判断)がわかりません。 どのように書き込むのでしょうか?

  • EXCEL マクロ

    こんにちは。 今マクロを作成しております。 A1~D1の中の数字の最小値・最高値に罫線の斜め線を入れたいのですが、 例) A1  B1  C1  D1    10  15  15   8   下記のマクロだと上記のように同じ数字があるとその両方に斜め線が入ってしまいます。 斜め線を入れたいのは、最小値1つと最高値1つで、上記の場合B1とC1が最高値なので C1に斜め線を入れたいのです。 仮にA1とD1が同数で最小値または最高値の場合D1に斜め線    B1とD1が同数で最小値または最高値の場合D1に斜め線 (同数の場合より左側にあるセルに斜め線) どのようにしたらいいのかアドバイスいただけますか? Sub 上下カット() Dim MyR As Range Dim MyMax, MyMin With ActiveSheet MyMax = WorksheetFunction.Max(.Range("A1:D1")) MyMin = WorksheetFunction.Min(.Range("A1:D1")) For Each MyR In .Range("A1:D1") If MyR.Value = MyMax Or MyR.Value = MyMin Then MyR.Borders(xlDiagonalUp).LineStyle = xlContinuous End If Next End With End Sub

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

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (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

    教えて下さい、EXECL以下の異なるVBA (A>,B>)が2つあります、同じシートでそれぞれ動くようにさせたいです1つに合わせる事は出来ないでしょうか? 当方初心者の為わかりません教えて下さい。 A> Private Sub Worksheet_Change(ByVal Target As Excel.Range) If Target.Address(0, 0, xlA1, 0) <> "A1" Then Exit Sub With Range("F9:I9,K17:K36").Borders(xlDiagonalUp) If Left$(Target.Value, 1) = "S" Then .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic Else .LineStyle = xlNone End If End With End Sub B> Private Sub Worksheet_Change(ByVal Target As Range) With Sheet2 Select Case Target.Address Case Is = "$D$1" .Range("A1").Insert Shift:=xlDown .Range("A1").Value = Target.Value Case Is = "$D$2" .Range("B1").Insert Shift:=xlDown .Range("B1").Value = Target.Value End Select End With End Sub

  • VBA罫線

    VBA罫線 a = 9 With Range(Cells(3, 2), Cells(a, 5)) .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeBottom).LineStyle = xlContinuous .Borders(xlEdgeRight).LineStyle = xlContinuous .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous .Borders(xlInsideHorizontal).LineStyle = xlContinuous End With どこのワークシートかを指定する場合はどうしたらいいのでしょうか?

  • エクセルVBA 行列の数を指定して罫線を引くマクロ教えてください

    下のようにSheet1に罫線を引く「開始セル(左右端)」「行」「列」が書いてあります。 この条件で罫線をSheet2に引くようにしたいのですが、変数の設定の仕方などが分かっていないようで、できません。教えていただけないでしょうか。マクロの記録をとったところ、下のようになりました。よろしくお願いします。 開始セル Sheet2!A1・・・Sheet1のB1セル 行 8・・・Sheet1のB2セル 列 2・・・Sheet1のB3セル Sub borders() Range("A1:B8").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 .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End Sub

  • ,最終行からA1の間に罫線を引く方法は?

    お願いします。 罫線を引くマクロを書きました。(下記) 範囲のJ26は最終行です。この表のデータ量は変化します。 最終行がJ26とは限りません。Z5000かもしれません。 その範囲に罫線をひくのですが、マクロ的に最終行を認知してA1 まで罫線を引くマクロをどう記述すればよいのか教えてください。 Sheets("読み込み").Select Range("A1:J26").Select Range("J26").Activate 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 .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With Range("J3").Select End Sub

専門家に質問してみよう