• ベストアンサー

エクセル マクロ VBA 罫線 文字列

職場で使う表をVBAマクロを用いて罫線作成をしています。 前任者のアレンジを頼まれたのですが前任者に連絡が取れず困っています。 表の特徴は以下のようになります。 ・A列を飛ばし、B列から2列飛びで文字を記入 ・b2=曜日、b3=1、b4=2、b5=3、b6=4、b7=空白のセットが曜日ごとに2セット×7日分 この表を ・b2=曜日、b3=-3、b4=-2、b5=-1、b6=0、b7=1、b8=2、b9=3、b10=4、b11=空白のセットが曜日ごとに2セット×7日分 に変更したいのですが空欄の場所がずれてしまい上手くいきません。 原本のマクロは以下です。 ---------------------------------------------------------------- Sub 罫線作成() Range(Cells(4, 1), Cells(86, 22)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ch1 = "月火水木金土日" For i = 4 To 76 Step 12 n1 = (i + 8) \ 12 Range(Cells(i, 1), Cells(i + 10, 22)).Select With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .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 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With 'Cells(i, 1) = Mid(ch1, n1, 1) For i2 = 2 To 20 Step 3 For i3 = i To i + 10 nb1 = (i3 + 8) Mod 12 If nb1 = 0 Or nb1 = 6 Then Cells(i3, i2) = Mid(ch1, n1, 1) If nb1 = 1 Or nb1 = 7 Then Cells(i3, i2) = 1 If nb1 = 2 Or nb1 = 8 Then Cells(i3, i2) = 2 If nb1 = 3 Or nb1 = 9 Then Cells(i3, i2) = 3 If nb1 = 4 Or nb1 = 10 Then Cells(i3, i2) = 4 Next Next Next End Sub ---------------------------------------------------------------- 4行目から142行目まで使用することは分かっているのですが… どうかご助力お願いします。

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

  • ベストアンサー
  • nekotaru
  • ベストアンサー率50% (22/44)
回答No.2

こんな感じでしょうか? Sub 罫線作成2() Range(Cells(4, 1), Cells(142, 22)).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone ch1 = "月火水木金土日" For i = 4 To 134 Step 20 n1 = (i + 16) \ 20 Range(Cells(i, 1), Cells(i + 18, 22)).Select With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .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 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With 'Cells(i, 1) = Mid(ch1, n1, 1) 'これはおk(よこ For i2 = 2 To 20 Step 3 'これはたて For i3 = i To i + 18 nb1 = (i3 + 16) Mod 20 If nb1 = 0 Or nb1 = 10 Then Cells(i3, i2) = Mid(ch1, n1, 1) If nb1 = 1 Or nb1 = 11 Then Cells(i3, i2) = -3 If nb1 = 2 Or nb1 = 12 Then Cells(i3, i2) = -2 If nb1 = 3 Or nb1 = 13 Then Cells(i3, i2) = -1 If nb1 = 4 Or nb1 = 14 Then Cells(i3, i2) = 0 If nb1 = 5 Or nb1 = 15 Then Cells(i3, i2) = 1 If nb1 = 6 Or nb1 = 16 Then Cells(i3, i2) = 2 If nb1 = 7 Or nb1 = 17 Then Cells(i3, i2) = 3 If nb1 = 8 Or nb1 = 18 Then Cells(i3, i2) = 4 Next Next Next End Sub

hirose0209
質問者

お礼

早速の回答、ありがとうございました。 データはほぼ原型のまま、改変することが出来ました! ありがとうございます。

その他の回答 (1)

  • pauNed
  • ベストアンサー率74% (129/173)
回答No.1

こんにちは。 VBAで行わないといけない理由があるのでしょうか? 手作業ででも1度つくってしまって、それを雛形として、作成したい時にコピーすれば良いだけなのでは? #でも、まぁ、一応。 Sub test()   Dim v      With ActiveSheet     .Range("A4:V142").Borders.LineStyle = xlNone     v = [{"=TEXT(INT(ROW(A40)/20),""aaa"")";-3;-2;-1;0;1;2;3;4;""}]     With .Range("B4:B13")       .Formula = v       .Offset(10).Formula = v     End With     With .Range("A4:V22")       .Borders.LineStyle = xlContinuous       .BorderAround Weight:=xlMedium       .Copy Destination:=.Worksheet.Range("A24,A44,A64,A84,A104,A124")     End With     With .Range("B4:B142")       .Value = .Value       .Copy Destination:=.Worksheet.Range("E4,H4,K4,N4,Q4,T4")     End With   End With End Sub 罫線部は単純コピーしてます。 コピー先に何かデータがはいっていたら消しますから、書式の貼り付けに変える必要があります。 その辺は工夫してみてください。

hirose0209
質問者

お礼

実はこの直後にこのデータからとある文字列を抜き出して別のシートに吐き出すというマクロがあります。 先程のものはそれと連動させて使わなければならないのでなるべく原型のままでなくてはならない様で…正直困り果ててしまいました。 ですがおかげさまで解決いたしました! ありがとうございます。

関連するQ&A

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

    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 行列の数を指定して罫線を引くマクロ教えてください

    下のように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

  • 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 よろしくお願いします

  • 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

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

    罫線を引き、配色し、文字に色を付ける。 このコードをどのようにダイエットすればよいのでしょうか? 罫線のマクロはこんなに大きいのですか? 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

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

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

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

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

  • マクロでマスを作成

    入力された数のマスを作成したいのですが、うまくマクロが作動しません。 (1)A1には作成したいマスの数が入力される (2)B1~B20に(1)で入力された数の□(しかく)で囲まれたマスが出来る  ※最高20列 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 (3)(2)では最高20マスしか出来ないので、60個以上のマスの場合  C1~C20、D1~D20に作成される よろしくお願いします

  • このマクロを高速化させるにはどうすればいいですか?

    VBA初心者で、とりあえず頑張って作ってみました。以下のVBAでの修正点を教えてください。 (英単語の小テスト用につくりました。) Range("C3:E22,H3:J22").Select Selection.Font.ColorIndex = 2 Selection.Interior.ColorIndex = 2 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlNone Selection.Borders(xlEdgeTop).LineStyle = xlNone Selection.Borders(xlEdgeBottom).LineStyle = xlNone Selection.Borders(xlEdgeRight).LineStyle = xlNone Selection.Borders(xlInsideVertical).LineStyle = xlNone Selection.Borders(xlInsideHorizontal).LineStyle = xlNone Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone ' ここまでは、共通の動作 Range("A1").Select ActiveCell.FormulaR1C1 = "20" ' 問題数に応じて、数字を変更 Range("C3:E22,H3:J22").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.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With ' 罫線を引く Range("D3:E22,I3:J22").Select Selection.Font.ColorIndex = 1 ' 文字を黒くする Range("C3:C22,H3:H22").Select Selection.Interior.ColorIndex = 16 ' セルをグレーにする Rows("3:18").Select Selection.RowHeight = 31.5 ' セルの幅を指定 ActiveSheet.PageSetup.PrintArea = "$B$1:$K$25" ' 印刷範囲を指定 Range("U3:U42").Select Selection.ClearContents ' 四線を消去 Range("D3:D4").Select Calculate ' 再計算完了 宜しくお願いします。 また、このVBAは慣れている人から見ると何点くらいですか?(感覚で結構です)

専門家に質問してみよう