• 締切済み

Excelでの罫線のみのコピーについて

エクセルのVBAを使って罫線のみのコピーコマンドを作ろうかと考えています。選択範囲をForEachで、Selectionループをかけてとりあえず配列にパラメーターを取り込み、同じくループで、Rangeに貼り付けようと考えました。が、3次元配列の動的割り当てが上手く行きません。(ローカルウィンドで見ると、配列が定義されていませんが、エラーも出ません)何故でしょうか?どなたか教えて頂けないでしょうか?また、そんな方法よりも、こうした方がいいよ!!ってのがありましたら教えて頂けないでしょうか?宜しくお願いします。 Dim MyLine() As Variant Dim I As Integer ReDim MyLine(Selection.Count, 3, 2) I = 0 For Each myrang In Selection MyLine(I, 0, 0) = myrang.Borders(xlEdgeTop).LineStyle '上端 MyLine(I, 0, 1) = myrang.Borders(xlEdgeTop).Weight '上端 MyLine(I, 0, 2) = myrang.Borders(xlEdgeTop).ColorIndex '上端 I = I + 1 Next とりあえず上端のみを載せました。実際によ全てのパラメータを配列に取り込むつもりなので、3次元にしています。

みんなの回答

  • NNAQ
  • ベストアンサー率56% (104/184)
回答No.4

雛形に表示形式・配置・フォント・罫線など、全ての書式を設定して、 書式のみコピー&ペーストするのでは、だめなのでしょうか? '#3のリンク先をちょっといじっただけの罫線のみのコピーコマンド Sub test() Dim myrang As Range 'In Selection コピー元 Dim rng As Range '貼り付け先 Dim b As Border '一本の罫線 Dim i As Integer, j As Integer 'ループ用 '貼り付け先のシートと左上セルを指定 Set rng = Sheets(2).Range("b2") With Selection Set rng = rng.Resize(.Rows.Count, .Columns.Count) End With i = 1 For Each myrang In Selection '罫線1本ずつ処理(斜め罫線含む) '上下左右だけなら j = xlEdgeLeft To xlEdgeRight 'または j = 7 to 10(#1の回答を参照) For j = xlDiagonalDown To xlEdgeRight Set b = myrang.Borders(j) If b.LineStyle <> xlNone Then '「罫線なし」でないなら With rng.Cells(i).Borders(j) '貼り付け先に罫線設定 .Weight = b.Weight .ColorIndex = b.ColorIndex .LineStyle = b.LineStyle End With End If Next j i = i + 1 Next myrang End Sub

全文を見る
すると、全ての回答が全文表示されます。
  • KenKen_SP
  • ベストアンサー率62% (785/1258)
回答No.3

こんなサンプルがありました。参考になると思います。 ■参考ページ 後段の記事番号 E03M121 にある kBorderCopyPaste 関数です。 http://homepage2.nifty.com/kmado/ke_m13.htm フレーム切れの案内で申し訳ありません TOP ページは下記です。 ■TOPページ http://homepage2.nifty.com/kmado/

全文を見る
すると、全ての回答が全文表示されます。
  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.2

なんで難しい途を選ぶのだろう。 別シートにコピーして、編集ークリアー数式と値およびコメント の操作をマクロの記録をとれば良いのでは。 エクセルVBAであって、VBではないのだから、もっとエクセルの操作のことも絡んで、良い方法はないか勉強ないし質問をすべきです。 3次元配列を考えるなんて、素人的な発想をすぐ言語の仕様(できるという知識を得て)に短絡させたものだと思う。VBAの解説本を読めば判るように、普通はエクセルVBAの課題では、全く使われていない。 配列そのものもシートのセル(およびシート群)が、見方によっては配列的なので、シート(群)対象にはほとんど必要ない。 罫線Borderはセルの4辺などに8つもあり、Borders,Borderaroundもあり、罫線属性としてスタイルや太さ色もあり、セルごとに元シートのセルのそれらを個別に取得して、別シートに再現するのはコード上複雑になると思う。 プログラムするとき、発想(ロジック)が我流でないか反省するのが大切でしょう。

vba_minarai
質問者

補足

VBAでやろうとしたのには、理由があって、表の中でカット&ペーストを繰り返すと、表の罫線がぐちゃぐちゃになってしまいます。単純にシートを使ってのコピペすると、もとからあったデータが上書きされてしまい消えてしまいます。そこで、一々手動で罫線を直していると時間がかかるので、マクロで、雛形の罫線のみを複写しようかと考えました。 imogasiさんのアドバイスを参考に逆に、一度雛形をTmpシートに複写して、それに、元のシートのセルの値、数式、コメントなどをコピーした後に、元のシートに上書きする方法を考えて見ます。 何かご忠告があれば、指導願います。

全文を見る
すると、全ての回答が全文表示されます。
回答No.1

とりあえず、 >ローカルウィンドで見ると、配列が定義されていませんが、エラーも出ません は、ちゃんと配列は定義されているみたいです。 >ReDim MyLine(Selection.Count, 3, 2) は、ReDim MyLine(Selection.Count-1, 3, 2)の方が美しいです。(0から始めているので) それと、せっかく変数の宣言をしているのだから、Dim myrang As Range も宣言した方がいいと思います。 先頭に、Option Explicit を入れると、煩わしいけど、予想外のエラーが減ります。 本題に戻って、 >3次元配列の動的割り当てが上手く行きません。 3次元配列は必要ないと思います。 記憶する変数の型をBordersにすれば、以下になります。 Dim b() As Borders Dim i As Integer Dim myrang As Range ReDim b(Selection.Count - 1) i = 0 For Each myrang In Selection Set b(i) = myrang.Borders i = i + 1 Next ただし、コピー先のセルのbordersを設定する時は、セル.Borders=??の形はできないみたいです。(やってみた限りですが) 設定する時は、1セルについて、 with cells(?,?).Borders(xlEdgeTop) .LineStyle=b(?).Borders(xlEdgeTop).LineStyle .Weight =b(?).Borders(xlEdgeTop).Weight .ColorIndex =b(?).Borders(xlEdgeTop).ColorIndex end with with cells(?,?).Borders(xlEdgeBottom) ... end with with cells(?,?).Borders(xlEdgeRight) ... end with with cells(?,?).Borders(xlEdgeLeft) ... end with と言う作業が必要です。 でも、 xlEdgeLeft = 7 xlEdgeTop = 8 xlEdgeBottom = 9 xlEdgeRight = 10 なので、変数でループさせるとコードが短くなります。

vba_minarai
質問者

お礼

ご丁寧なご指導有難う御座います。 変数でループの点など大いに参考にしたいと思います。 Dim b() As Bordersについては、構造体でしょうか?大変参考になりました。仰るとおり、取得だけでも、構造体の形がとれれば簡素化できますね!!でも、設定が出来ないなんて、不便ですね?? Option Explicit は入れています。コードが抜粋なので、転写し忘れました。今後とも宜しくお願い致します。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

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

  • 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

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

    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

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

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

    職場で使う表を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行目まで使用することは分かっているのですが… どうかご助力お願いします。

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

  • excelのマクロをつかったコピー

    excelのマクロを使ってもとのシートのデータをコピーして新しいブックに貼り付けを行い、ブック名を指定して保存させることを繰り返したい。 excelのマクロで以下の作業が出来ないかと考えております。 1.選択したシートのAC(i)~CW(i)をコピー (i=3,n) 2.ブック『雛形』(コピー先のテンプレートブック)を開く 3.開いたブックのSheet1のB3~BU3に値をペースト 4.同様にSheet1のB3~BU3に書式をペースト 5.ブック名を指定して保存。(ブック名は"シート名""-""i(桁指定3桁)") 6.2~5を繰り返す。iはコピー元のデータがブランクになるまで繰り返す。 なお、コピー元のAC(i)~CW(i)はいくつかの結合セルとなっており、同シート内のB(i)~P(i)を参照して値を表示する関数を組んだデータとなっています。 作成したいブック数が3000ファイル程度になる為、手作業で行うには時間がかかりすぎるため何とか作業効率をあげたいと考えております。 操作を行ってみて記録したマクロを自分でいじってループさせられないかやっていますがうまくいきません。 Sub Macro1() Dim i As Long For i = 3 To 100 Sheets("Aエリア").Select Range("AD(i):CW(i)").Select Selection.Copy Workbooks.Open Filename:="(雛形ファイル名).xls" Range("B4").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Range("B2:BU4").Select Range("B4").Activate Application.CutCopyMode = False Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone 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 Range("AZ6").Select ファイル名 = "(新規保存ブック頭文字)" Application.Dialogs(xlDialogSaveAs).Show (ファイル名) Next 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

Montereyでドライバ削除方法
このQ&Aのポイント
  • macOS Montereyでのドライバ削除方法についてのご質問です。
  • macOS Montereyを新規インストールした後、プリンタードライバとスキャナードライバをインストールしたらカーネルパニックに陥りました。
  • Q&Aの方法ではTWAINが見つからないため、全てのドライバを削除する方法を教えてください。
回答を見る

専門家に質問してみよう