プロが教える店舗&オフィスのセキュリティ対策術

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

A 回答 (3件)

先ず、Sheet1の条件の入れ方を以下のように変えるべきです。



B1 : Sheet2 (シート名)
B2 : A1   (開始セル)
B3 : 8    (行数)
B4 : 6    (列数)

'-------------------------------------- 
Sub Test()
Dim myRange As Range
With Sheets(Range("B1").Value)
 Set myRange = .Range(Range("B2").Value).Resize(Range("B3").Value, Range("B4").Value)
   With myRange.Borders
     .LineStyle = xlContinuous
     .Weight = xlThin
     .ColorIndex = xlAutomatic
   End With
End With
End Sub
'------------------------------------------

以上。
 
 
    • good
    • 0
この回答へのお礼

分かりづらい質問だと自分でも後で読み直し思いましたが、このようにしたいと思っていました。ありがとうございました。

お礼日時:2008/04/02 22:16

質問がとてもわかりづらいのですが


> 開始セル Sheet2!A1・・・Sheet1のB1セル
> 行 8・・・Sheet1のB2セル
> 列 2・・・Sheet1のB3セル

Sheet2!A1から、Sheet1のB2にある数値の行、Sheet1のB3にある数値の列までの範囲に罫線を入れるということでしょうか?
それなら、

Sub borders()

Dim r As Integer, c As Integer

With Sheets("Sheet1")
r = .Range("B2").Value
c = .Range("B3").Value
End With

With Sheets("Sheet2")
With .Range(.Cells(1, 1), .Cells(r, c)).borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With

End Sub

でどうでしょう?
    • good
    • 0
この回答へのお礼

ありがとうございました。

お礼日時:2008/04/02 22:13

>行列の数を指定して罫


どういう風な範囲指定をするのか。
>Range("A1:B8").Select
のところを相対化するのだろうが、どういう風に指定するのか
左上隅セル+行数+列数
ぐらいしか指定の方法がないかな
左上隅セル=C4
行数=5
列数=3
なら
Sub test02()
leftSUMIC = "C4"
r = Range(leftSUMIC).Row
C = Range(leftSUMIC).Column
Range(Cells(r, C), Cells(r + 5 - 1, C + 3 - 1)).Select
End Sub
===
しかし上記のようなことより、Application.Inputboxの Type:=8
(範囲をユーザー指定)を検討してはどうでしょう。
    • good
    • 0
この回答へのお礼

ありがとうございました。

お礼日時:2008/04/02 22:11

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!