VBAエラー:セルの範囲設定エラー

このQ&Aのポイント
  • VBAを使用して上下カットの処理を行う際、オブジェクト変数またはWithブロック変数が設定されていないため、エラーが発生します。
  • エラーメッセージ「オブジェクト変数またはWithブロック変数が設定されていません。」が表示される場合、範囲設定に誤りがある可能性があります。
  • 処理中に使用する範囲オブジェクトを正しく設定してください。
回答を見る
  • ベストアンサー

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ブロック変数が設定されていません。”と出ます。 どうしたらいいですか?

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

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

こちらの環境(Excel2010)では正常に動きます。 提示のコードはご自身のVBAエディタらのコピペですか? そうでないなら単純な入力ミスの可能性が高いです。 あれ?エラーの種類が違うってことはもしかしてru-0724とnakekoは別人物? 【ru-0724】 http://okwave.jp/qa/q8773730.html http://okwave.jp/qa/q8773907.html 【nakeko】 http://okwave.jp/qa/q8773746.html http://okwave.jp/qa/q8773920.html

ru-0724
質問者

お礼

申し訳ありません。 コピーしてそのまま添付しました。 このエラーのあと試行錯誤していたら、別のエラーが出たため、質問させていただきました。 紛らわしいことをしてしまい、大変申し訳ありません。 再度、入力し直してみたいと思います。 ありがとうございす。

関連する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 を実行すると、 MX.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

  • エクセル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 の書き込み方(マスタシートの斜線判断)がわかりません。 どのように書き込むのでしょうか?

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

    以下のプログラムでは、選択したシートのみマクロが動作しています。ネット検索で見よう見まねで作ったため何がまちがっているのかわかりません。ご教示いただけるとありがたいです。 ・月の予定表で利用者が休みの日に斜線を引くマクロ ・入力ミスを防ぐためシート保護をしている 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罫線 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 どこのワークシートかを指定する場合はどうしたらいいのでしょうか?

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

    ワークシートのセルの書式設定の罫線をマクロでひく。 下記マクロを実行すると  (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を使い表を作っているのですが、入力フォームで日付を入力しコマンドボタンを押すと別ブックになり、予め用意してある表に入力した日付から1ヶ月の日付が貼り付けるというのをやっています。 マクロを記録し試してみたところ構文エラーが出てしまいました。 日付を貼り付けるところまでできたのですが・・・ 表には線がすでに入っています。 入力された最後の日付のところで太線で表を閉めたいのですができますでしょうか。 構文エラーになったソースを貼り付けます。 Range((4,"i"+12):(10,"i"+12)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic 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

専門家に質問してみよう