R[-N]やC[-N]という記述も出来ます。
| 一つのセルの指定 |
Cells(行番号,列番号).??? Cells(3,4).???
Range("セル名").??? Cells("D3").??? |
|---|---|
| セルの範囲の指定 |
Range("セル名:セル名").??? Range("B5:J10").???
Range(Cells(行番号1, 列番号1), Cells(行番号2, 列番号2)).???
Range(Cells(5, 2), Cells(10, 10)).??? |
| セル領域のクリア |
Range(Cells(1,2),Cells(3,4)).Clear |
| セルのコピー |
Sheets(1).Range("B3:G7").Copy Sheets(2).Range("B3")
|
Sheets(1).Select
Range("B3:G7").Select
Selection.Copy
Sheets(2).Range("B3:G7").Paste
Application.CutCopyMode = False
|
|
| セルの式指定 A1形式とR1C1形式 |
Range("D5").Select
ActiveCell.Formula = "=SUM(C1:C4)"
|
Range("C5").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
|
|
Cells(68, 10).Select ActiveCell.FormulaR1C1 = "=R[-6]C-R[-4]C-R[-2]C" |
|
| アクティブセルの位置記憶 |
InputRow = ActiveCell.Row InputColumn = ActiveCell.Column |
| 指定セルへのカーソル移動 |
Cells(InputRow, InputColumn).Select |
| セルの結合 |
Range("B3:C4").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
|
| セルの結合解除 |
Range(Cells(1, 1), Cells(3, 108)).Select
Range("A2").Activate
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
|
| セルの文字をセンター配置 |
Range("B3:C4").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
|
Sub Sample1()
Dim i As Integer
Dim LoopArea As Range
Set LoopArea = Selection
For i = 1 To LoopArea.Count
LoopArea.Cells(i) = "AA"
Next
End Sub
Sub Sample1()
Dim RowCnt, ColCnt, StartRow, StartColumn As Integer
Dim Max_Row, Max_Column As Integer
Dim LoopArea As Range
Dim SelectArea As String
SelectArea = Selection.Address
RowCnt = Selection.Row
ColCnt = Selection.Column
Set LoopArea = Selection
StartRow = LoopArea.Cells(1).Row
StartColumn = LoopArea.Cells(1).Column
Max_Row = LoopArea.Cells(LoopArea.Count).Row
Max_Column = LoopArea.Cells(LoopArea.Count).Column
End Sub
実行結果は以下の通り。SelectArea : "$B$3:$F$10"
RowCnt : 3
ColCnt : 2
StartRow : 3
StartColumn : 2
Max_Row : 10
Max_Column : 6
Range("R26") = Date
Selection.NumberFormatLocal = "yyyy""年""m""月""d""日"";@"
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
------ 単位の設定 ------
Columns("I").Select
Selection.NumberFormatLocal = "#,##0" + "uP"
With Range("F" & I).Interior
.ColorIndex = 36
End With