• ベストアンサー

.NET上でエクセル上に罫線を引く

VB.NETを独学で勉強しているものです .NETのプログラミングでEXCELを開いてセルに数値を入力するところまではできるようになったのですが、罫線を引こうとするとうまくいきません。 ws.Range(Karist).Borders.LineStyle = xlContinuous のところの「xlContinuous」の下部に波線が出て、変数が宣言されていないと出るのですが、EXCELの定数はどのように宣言すればよいのでしょうか? よろしくお願いいたします

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

  • ベストアンサー
  • keitakuna
  • ベストアンサー率42% (6/14)
回答No.2

罫線を引く   ws.Range(Karist).Borders(1).Weight = 1      Borders(1) → セルの左      Borders(2) → セルの右      Borders(3) → セルの上      Borders(4) → セルの下      Weight = 1 → 細線 Weight = 2 → 普通      Weight = 3 → 太線      Weight = 4 → 極太線 罫線の型    ws.Range(Karist).Borders(1).LineStyle = 1 罫線の色    ws.Range(Karist).Borders(1).Color = QBcolor(2)

nanbaya2
質問者

お礼

実際に試したところ、うまく行きました。大変参考になりました。応用として、書いているラインを消すということもうまくいき、大変役立っています。どうもありがとうございました。

その他の回答 (3)

  • Wendy02
  • ベストアンサー率57% (3570/6232)
回答No.4

こんにちは。 Excel側の組み込み定数を使うなら、参照設定すればよいです。そうでなければ、Excelを開いて、実際のコードで定数を調べるなり、#2さんの数字を使うなりすればよいと思います。

nanbaya2
質問者

お礼

参考になりました。どうもありがとうございました

  • herencia
  • ベストアンサー率56% (120/211)
回答No.3

ws.Range(Karist).Borders.LineStyle = Excel.XlLineStyle.xlContinuous とすると定数を利用できます。 定数でなく直接値を入力するのであれば1でいいです。 メニューの「表示」->「オブジェクブラウザ」で調べるとどこに定義されているかがわかります。

nanbaya2
質問者

お礼

参考になりました。どうもありがとうございました

  • n-akina
  • ベストアンサー率31% (75/238)
回答No.1

こんにちは。  Excelの定数であろうが、VBの定数であろうが、宣言方法は同じだと思います。xlContinuousをいくつに設定したらいいかというのであれば、ヘルプのMicrosoft Excel Visual Basicリファレンスの列挙の中に記述されていると思います。 では。

nanbaya2
質問者

お礼

参考になりました。どうもありがとうございました

関連するQ&A

  • エクセルVBAにおける罫線の色指定について

    エクセルVBAの初心者です。 使用機種はWindows VistaでExcel2007です。 後に示すコードではどうして("B4:H7")までの下罫線と ("C4:H8")までの左罫線が青色にならず、黒色のまま になるのでしょうか?("B4:H7")、("C4:H8")ともに 罫線の色は青色にしたいと思っています。 原因と対処方法をご存知の方ご教示願います。 画像も添付しておりますので、合わせてご参照ください。 Sub 日程表作成() Set WS1 = Worksheets("sheeT1") With WS1 .Range("B3") = "日" .Range("B3").Select End With Selection.AutoFill Destination:=Range("B3:H3") WS1.Range("B3:H3").Select With Selection.Borders(xlLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With With Selection.Borders(xlRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 5 End With '下線を二重線に変える Range("B3:H3").Select Selection.Borders(xlBottom).LineStyle = xlDouble WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous WS1.Range("B3:H8").RowHeight = 35 '日程表の中に横線を入れる WS1.Range("B4:H7").Select Selection.Borders(xlBottom).LineStyle = xlContinuous WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous '日程表の中に縦線を入れる WS1.Range("C4:H8").Select Selection.Borders(xlLeft).LineStyle = xlContinuous WS1.Range("B3:H8").BorderAround Weight:=xlThick, _ ColorIndex:=5, LineStyle:=xlContinuous '曜日のセルを塗りつぶす With WS1 .Range("B3:B8").Interior.ColorIndex = 38 .Range("C3:G8").Interior.ColorIndex = 19 .Range("H3:H8").Interior.ColorIndex = 19 .Range("B4").Activate 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 どこのワークシートかを指定する場合はどうしたらいいのでしょうか?

  • Excel VBAの罫線描画マクロをOpenOfficeCulcで実行。罫線が描画されない。

    Excel VBAで作成したマクロをOpenOffice.orgのCulcに単純移行して 実行してみたのですが、コードは実行されるのに罫線が描画されない 現象が発生しています。 罫線描画はCulcとExcelで互換が遅れている部分だと聞いていますが、 Culc独自のマクロで作成するしか回避方法はないのでしょうか? マクロは下記のような構成になっています。 REM ***** BASIC ***** Option VBASupport 1 Sub draw_line() With Worksheets("drawline") .Range("B2:E8").Borders(xlEdgeLeft).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeTop).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeBottom).LineStyle = xlContinuous .Range("B2:E8").Borders(xlEdgeRight).LineStyle = xlContinuous .Range("B2:E8").Borders(xlInsideVertical).LineStyle = xlContinuous .Range("B2:E8").Borders(xlInsideHorizontal).LineStyle = xlContinuous End With End Sub

  • AccessVBAでExcelを起動し、罫線を引きたいのですが、Exc

    AccessVBAでExcelを起動し、罫線を引きたいのですが、ExcelVBAの罫線を引く関数をAccessVBAで記述するとエラーになります。どういう記述をすればよいのでしょうか? (例)   以下の様にすると、例えば「xlNone」は宣言されていない等のエラーになります。   (ちなみに、Excelの起動と値の入力はできています) Range("A4:C6").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With

  • エクセルでデータがある部分だけ罫線で囲いたいです。

    エクセルでデータがある部分だけ罫線で囲いたいです。 エクセルのファイルを開いて、データのある部分だけを罫線で囲みたいです。 データーは常に列数も行数も違います。 マクロの記録で行ったら、以下のようになりました。 もう少し短い文章ではできないでしょうか? Sub Macro1() ' ' Macro1 Macro ' マクロ記録日 : 2010/9/22 ' Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).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

  • Excel VBAの自動メンバ表示

    Excel VBAで Range("A1").Borders.LineStyle = xlContinuous と記述する時、BordersやLineStyleは自動メンバ表示で選べるのですが、XlContinuousは自動で表示されず、いちいち本を見て入力しないといけません。 この最後の定数は表示されないものなのでしょうか?覚えるしかないのでしょうか?

  • エクセルVBA 作動させる度に罫線の種類を変える

    エクセル(2007) VBAで、マクロを作動させる度に選択した範囲の 罫線を変更したく、下記のようなマクロを組みましたが1種類の線しか書けません。。。 どのように修正すればよいか教えて頂きたいです。 何卒宜しくお願い致します。 ----------------------------------- Sub 罫線チェンジ() Select Case ActiveCell.Borders.LineStyle Case xlContinuous Selection.Borders.LineStyle = xlHairline Case xlHairline Selection.Borders.LineStyle = xlDot Case xlDot Selection.Borders.LineStyle = xlDouble Case xlDouble Selection.Borders.LineStyle = xlNone Case Else Selection.Borders.LineStyle = xlContinuous End Select End Sub -----------------------------------

  • エクセル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

  • 表に罫線を最終列まで引きたい

    windows7 とExcel2007でマクロ作成中の初心者です。 最初に、D2:E34を選んで罫線をひき、列を2列移動して、また罫線を引きます。 これをデータのある最終列まで繰り返したいのですが、うまくいきません。 どうしたらよろしいでしょうか。 Sub 勤怠に罫線() Dim n As Integer For n = 1 To 5 勤怠に罫線 Selection.Offset(0, 2).Select 勤怠に罫線 Next n End Sub 以下はマクロの自動記録でコードを書きました。 Sub 勤怠に罫線() Range("D2:E34").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlMedium End With Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Range("D2:E34").Select 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