VBでExcelファイルの編集方法

このQ&Aのポイント
  • VBを使用してExcelファイルを作成し、矩形で罫線を引く方法について教えてください。
  • Excelファイルを編集する際に、VBを使用して罫線を引くと一本線しか表示されない問題について、四角く矩形で罫線を引く方法を教えてください。
  • VBでExcelファイルの範囲を指定して罫線を引くと、矩形ではなく一本線しか表示されません。四角く矩形で罫線を引く方法をお教えください。
回答を見る
  • ベストアンサー

VBでExcelファイルの編集

VBでExcelファイルを作成しようとしていますが、罫線を引くとき、Rangeで範囲を指定しているんですが矩形でかかれず、一本線だけが書かれてしまいます。 どうしたら四角く矩形で罫線が書かれるか教えてください。 Set objExcel = CreateObject("Excel.Application") 'エクセルオブジェクト作成 objExcel.Visible = False 'エクセルを非表示で objExcel.Workbooks.Add '新規に作る With objExcel.Sheets("Sheet1").Range("a19:k29").Borders(1) .LineStyle = xlContinuous .Weight = 4 .ColorIndex = xlAutomatic End With

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

  • ベストアンサー
  • papayuka
  • ベストアンサー率45% (1388/3066)
回答No.1

VBは持ってないので的を得ているか解りませんが、、 ExcelVBAだと下記の感じでしょうか? Sub Test() Set objExcel = ThisWorkbook For i = 7 To 10  With objExcel.Sheets("Sheet1").Range("A19:K29").Borders(i)     .LineStyle = xlContinuous     .Weight = 4     .ColorIndex = xlAutomatic  End With Next i End Sub

itsupy
質問者

お礼

ありがとうございます! できました。

その他の回答 (2)

  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.3

>どうしたら四角く矩形で罫線が書かれるか. BorderAroundを使います。 BorderAroundに名前付き引数をとるので、下記のようになります。WithとEndWithのプロパティの書き方とは変ります。 エクセルの世界に入るとエクセルVBAで、それを使うときはVBAの本などを参照しましょう。Borders(xlEdgeTop)ほか4つで囲む方法もあります。 Sub test01() Set objExcel = CreateObject("Excel.Application") 'エクセルオブジェクト作成 objExcel.Visible = True 'エクセルを表示で objExcel.Workbooks.Add '新規にブックを作る objExcel.Sheets("Sheet1").Range("a19:k29").BorderAround _ LineStyle:=xlContinuous, Weight:=4, ColorIndex:=xlAutomatic End Sub

itsupy
質問者

お礼

ありがとうございます。 かなり詳しく分かったような気がします。

  • bin-chan
  • ベストアンサー率33% (1403/4213)
回答No.2

EXCEL側で行いたいことをEXCEL上のマクロで記録して、 VBAエディタのコードをVBへコピー&ペーストしていけば どのように記述すればよいか確認しやすいですよ。 (もっとも余分なコードもありますが) コピペしたあと、ActiveSheetsをobjExcelに書き換えてください。 Borders(1)の()内の値が複数必要になることがわかりますよ。

itsupy
質問者

お礼

ありがとうございます。 よくわかりました。

関連するQ&A

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

    エクセルでデータがある部分だけ罫線で囲いたいです。 エクセルのファイルを開いて、データのある部分だけを罫線で囲みたいです。 データーは常に列数も行数も違います。 マクロの記録で行ったら、以下のようになりました。 もう少し短い文章ではできないでしょうか? 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

  • このマクロコードをダイエットするには?

    罫線を引き、配色し、文字に色を付ける。 このコードをどのようにダイエットすればよいのでしょうか? 罫線のマクロはこんなに大きいのですか? Withの使い方がよくわかりません。 よろしくお願い致します。 ------------ Sub Test1() Range("B1:O14").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 = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlHairline .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 With Selection.Interior .ColorIndex = 56 .Pattern = xlSolid End With Selection.Font.ColorIndex = 33 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

  • ,最終行から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

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

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

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

  • エクセルVBAで罫線挿入

    マクロの記録で下記のように記録されたものを簡潔にまとめるにはどのように記述したらいいでしょうか? Range("C3:F3").Select 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 .HorizontalAlignment = xlCenterAcrossSelection .VerticalAlignment = xlBottom End With

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

    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

  • VBA 選択範囲の中で、更に一番上の行を指定したい!

    タイトルの通りです。 選択範囲に罫線を引くマクロを作成しました。 外に太い枠線、中は点線を引き、選択範囲の一番上の行を灰色の塗りつぶしにしたいのです。 しかし、罫線はうまくいきましたが、一番上の行の指定がうまくできません。 Sub 罫線をひく() ' With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With ActiveWindow.SmallScroll Down:=12  ←ここを変えたいのですが、どうすればよいかわかりません。 With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.499984740745262 .PatternTintAndShade = 0 End With End Sub どうすればよいか、もしご存知の方がいらっしゃいましたら、ご指導ください。 よろしくお願いたします。

  • Excel2003枠を作るマクロ

    右側に空白の行を一つつくって枠を作りたいです。 たとえば、G100が一番右下とするとH100まで枠を作りたいのですが、きれいにかくにはどうしたらよいでしょうか? マクロ記録でやると、下のようになるのですが右下が100で有るとは限らないのでその行を定義する必要があると思うのですが、そのあたりがさっぱりわかりません。 よろしくお願いいたします。 Sub Macro1() Selection.End(xlDown).Select Range("H100").Select Range(Selection, Selection.End(xlUp)).Select Range(Selection, Selection.End(xlToLeft)).Select Range(Selection, Selection.End(xlToLeft)).Select Range(Selection, Selection.End(xlToLeft)).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

専門家に質問してみよう