Contents
作業中のワークシートのすべてのセルを表すRangeオブジェクトを返します。
- 構文
- Range = Application.Cells.[_Default]( [rowIndex, columnIndex] )
- 引数
- rowIndex
- 行番号を表す数値を指定する
- columnIndex
- 列番号を表す数値を指定する
- 戻り値
- Rangeオブジェクト
プログラム実行例
セルに文字列を入力(R1C1形式)
Excel = CREATEOLEOBJ("Excel.Application")
Excel.Visible = TRUE
ID = GETID("Microsoft Excel")
CTRLWIN(ID, ACTIVATE)
Excel.Workbooks.Add()
Excel.Cells(1, 1).Value = "UWSC"
// Excel.DisplayAlerts = FALSE
// Excel.Quit
- CREATEOLEOBJ
- Application.Visible プロパティ
- GETID
- CTRLWIN
- Workbooks.Add メソッド
- Application.Cells プロパティ
- Range.Value プロパティ
- Application.DisplayAlerts プロパティ
- Application.Quit
解説
- 1-2行目
- Excelを起動。
Excel = CREATEOLEOBJ("Excel.Application")Excel.Visible = TRUE
- 4-5行目
- Excelをアクティブ化
ID = GETID("Microsoft Excel")CTRLWIN(ID, ACTIVATE)
- 7行目
- 新規ブックの作成
Excel.Workbooks.Add()
- 8行目
- A1セルに「UWSC」と代入する。
Excel.Cells(1, 1).Value = "UWSC"
- 10行目
- 確認メッセージを表示しない。
// Excel.DisplayAlerts = FALSE
- 11行目
- Excelを終了する。
// Excel.Quit
気象庁のホームページから一月分の気温を取得しExcelでグラフを作成
CONST xlUp = -4162
CONST xlLineMarkers = 65
DIM year = 2020
DIM month = 8
DIM IE = IEBoot()
IE.Navigate("https://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=14&block_no=47412&year=" + year + "&month=" + month + "&day=&view=p1")
BusyWait(IE)
DIM array[-1][-1]
DIM element = IE.document.getElementById("tablefix1")
getTableData(element, array)
IE.Quit
DIM Excel = XLOPEN()
DIM SheetName = Excel.ActiveSheet.Name
XLSETDATA(Excel, array, "A1")
DIM row = Excel.Cells(Excel.Rows.Count, 1).End(xlUp).Row
DIM Charts = Excel.Charts.Add
WITH Charts
.ChartType = xlLineMarkers
.SeriesCollection.NewSeries
.HasTitle = TRUE
.ChartTitle.Text = "札幌 " + year + "年" + month + "月気温"
WITH .FullSeriesCollection(1)
.XValues = "=Sheet1!$A$5:$A$35"
.Name = "=<#DBL>最高気温<#DBL>"
.Values = "=Sheet1!$H$5:$H$35"
WITH .Format
.Fill.ForeColor.RGB = 255
.Line.ForeColor.RGB = 255
ENDWITH
ENDWITH
WITH .FullSeriesCollection(2)
.XValues = "=Sheet1!$A$5:$A$35"
.Name = "=<#DBL>最高気温<#DBL>"
.Values = "=Sheet1!$I$5:$I$35"
WITH .Format
.Fill.ForeColor.RGB = 16711680
.Line.ForeColor.RGB = 16711680
ENDWITH
ENDWITH
ENDWITH
//////////////////////////////////////////////////
// 【引数】
// IE : IEオブジェクト
// 【戻値】
//
//////////////////////////////////////////////////
PROCEDURE BusyWait(Var IE)
SLEEP(0.500)
DIM t = GETTIME()
TRY
REPEAT
DIM tm = GETTIME() - t
FUKIDASI("BusyWait:" + tm)
SLEEP(0.010)
IF tm >= 60 THEN BREAK
UNTIL !IE.Busy AND IE.readyState = 4
EXCEPT
IE = getIEObj(-1)
PRINT IE.document.URL + " のIEオブジェクトを取得しました。"
BusyWait(IE)
ENDTRY
FUKIDASI()
FEND
//////////////////////////////////////////////////
// 【引数】
// 文字列 or 数値 : 取得したいIEオブジェクトのタイトル・URLもしくは数値を指定
// 完全一致フラグ : (TRUE : 文字列が完全一致したものを取得、FALSE : 文字列の一部を含むものを取得)
// 【戻値】
// Internet Explorerオブジェクト
//////////////////////////////////////////////////
FUNCTION getIEObj(str, flg = FALSE)
DIM Shell = CREATEOLEOBJ("Shell.Application")
SELECT CHKNUM(str)
CASE TRUE
DIM cnt = 0
SELECT TRUE
CASE str > 0
FOR n = 0 TO Shell.Windows.Count - 1
TRY
IFB Shell.Windows.Item(n).Name = "Internet Explorer" THEN
cnt = cnt + 1
IFB str = cnt THEN
RESULT = Shell.Windows.Item(n)
EXIT
ENDIF
ENDIF
EXCEPT
ENDTRY
NEXT
CASE str = 5
SELEND
RESULT = ERR_VALUE
FEND
//////////////////////////////////////////////////
// 【引数】
// table : tableエレメント
// arr : 取得したデータを格納する配列(参照引数)
// 【戻値】
//
//////////////////////////////////////////////////
PROCEDURE getTableData(table, Var arr[][])
rowMax = table.rows.length - 1
colMax = 0
FOR row = 0 TO table.rows.length - 1
IFB table.rows(row).cells.length - 1 > colMax THEN
colMax = table.rows(row).cells.length - 1
ENDIF
NEXT
DIM arr[rowMax][colMax]
FOR row = 0 TO table.rows.length - 1
FOR col = 0 TO table.rows(row).cells.length - 1
n = 0
WHILE arr[row][col + n] ""
n = n + 1
WEND
arr[row][col + n] = table.rows(row).cells(col).innerText
IFB table.rows(row).cells(col).rowSpan > 1 AND table.rows(row).cells(col).colSpan > 1 THEN
rmax = table.rows(row).cells(col).rowSpan - 1
cmax = table.rows(row).cells(col).colSpan - 1
FOR r = 1 TO rmax
FOR c = 1 TO cmax
arr[row + r][col + c] = "←"
NEXT
NEXT
ENDIF
IFB table.rows(row).cells(col).rowSpan > 1 THEN
n = table.rows(row).cells(col).rowSpan - 1
WHILE n
arr[row + n][col] = "↑"
n = n - 1
WEND
ENDIF
IFB table.rows(row).cells(col).colSpan > 1 THEN
n = table.rows(row).cells(col).colSpan - 1
WHILE n
arr[row][col + n] = "←"
n = n - 1
WEND
ENDIF
NEXT
NEXT
FEND
//////////////////////////////////////////////////
// 【引数】
// InPrivate : InPrivateブラウズ・モードを有効にするときはTRUEを指定(デフォルトはFALSE)
// 【戻値】
// InternetExplorerオブジェクト
//////////////////////////////////////////////////
FUNCTION IEBoot(InPrivate = FALSE)
DIM IE
SELECT InPrivate
CASE TRUE
DOSCMD("start iexplore -private")
GETID("InPrivate - Internet Explorer - [InPrivate]", "IEFrame", -1)
IE = GETACTIVEOLEOBJ("InternetExplorer.Application","InPrivate - Internet Explorer - [InPrivate]")
CASE FALSE
TRY
IE = CREATEOLEOBJ("InternetExplorer.Application")
IE.Visible = TRUE
EXCEPT
EXEC("C:\Program Files\Internet Explorer\iexplore.exe")
GETID("Internet Explorer", "IEFrame", -1)
TRY
IE = GETACTIVEOLEOBJ("InternetExplorer.Application")
EXCEPT
IE = getIEObj(-1)
ENDTRY
ENDTRY
SELEND
RESULT = IE
FEND
- IEBoot
- Navigate
- BusyWait
- Document.getElementById メソッド
- getTableData
- XLOPEN
- Application.ActiveSheet プロパティ
- Worksheet.Name プロパティ
- XLSETDATA
- Application.Cells プロパティ
- Range.End プロパティ
- Range.Row プロパティ
- Application.Charts プロパティ
- Sheets.Add メソッド
- Excel.Charts
- Chart.ChartType プロパティ
- Chart.SeriesCollection メソッド
- Excel.SeriesCollection.NewSeries
- Series オブジェクト
- Chart.HasTitle プロパティ
- Chart.ChartTitle プロパティ
- Excel.ChartTitle.Text
- Excel.Chart.FullSeriesCollection
- FullSeriesCollection オブジェクト
- FullSeriesCollection.Item メソッド
- Series.XValues プロパティ
- Series.Name プロパティ
- Series.Values プロパティ
- Series.Format プロパティ
- FillFormat.ForeColor プロパティ
- Excel.ColorFormat.RGB
- Excel.LineFormat.FontColor
- 結果
人口の上位3位取得
上位3位まで赤文字にする。
使ったファイルのダウンロードは下記リンクからできます。

DIM Excel = ExcelBoot("都道府県別人口.xlsx")
CONST xlDown = -4121
CONST xlTop10Top = 1
WITH Excel
.Range("A:E").EntireColumn.AutoFit
FOR col = 3 TO 5
DIM Top10 = .Range(.Cells(2, col), .Cells(48, col)).FormatConditions.AddTop10
WITH Top10
.Rank = 3
.TopBottom = xlTop10Top
.Font.Color = 255
.Font.Bold = TRUE
ENDWITH
NEXT
ENDWITH
//////////////////////////////////////////////////
// 【引数】
// path : 開くファイルのパス名
// 【戻値】
// Excelオブジェクト
//////////////////////////////////////////////////
FUNCTION ExcelBoot(path = "")
DIM Excel = CREATEOLEOBJ("Excel.Application")
Excel.Visible = TRUE
IFB path = "" THEN
Excel.Workbooks.Add
ELSE
DIM FSO = CREATEOLEOBJ("Scripting.FileSystemObject")
IFB FSO.GetParentFolderName(path) = "" THEN
path = GET_CUR_DIR + "\" + path
ENDIF
Excel.Workbooks.Open(path)
ENDIF
RESULT = Excel
FEND
- ExcelBoot
- Excel.Range.EntireColumn.AutoFit
- Application.Range プロパティ
- Application.Cells プロパティ
- Excel.Range.FormatConditions
- FormatConditions オブジェクト
- Excel.FormatConditions.AddTop10
- Excel.Top10.Rank
- Excel.Top10.TopBottom
- Excel.Top10.Font
- Excel.Font.Color
- Font.Bold プロパティ
- 結果
既定プロパティ
ItemプロパティはRangeオブジェクトの既定プロパティなので、Cellsの引数にItemプロパティの引数であるrowIndex,columnIndexを直接指定することができます。
Cellsの記述例
例 | 説明 |
---|---|
Cells(1, 2) | B1セル |
Cells(2, “C”) | C2セル |
Cells | セル全体 |
Cells.Item(3, 4) | D3セル |
関連記事
- Application.ActiveCell プロパティ (Excel)
- アクティブな1つのセルを表すRangeオブジェクトを取得します。
- Application.Range プロパティ (Excel)
- セルまたはセル範囲を表すRangeオブジェクトを返します。
- Range.Merge メソッド (Excel)
- 指定したRangeオブジェクトから結合されたセルを作成します。
- Range.UnMerge メソッド (Excel)
- 結合された領域をそれぞれのセルに分割します。
- Range.Borders プロパティ (Excel)
- セルの罫線を表します。
- Range.Columns プロパティ (Excel)
- 指定した範囲の列を表すRangeオブジェクトを返します。
- Range.Count プロパティ (Excel)
- コレクションに含まれるオブジェクトの数を返します。
- Range.CurrentRegion プロパティ (Excel)
- 現在の領域を表すRangeオブジェクトを返します。
- Range.MergeCells プロパティ (Excel)
- セルの結合状態を設定または取得します。値を指定しなかった場合は結合状態を取得します。
- Range.Rows プロパティ (Excel)
- 指定した範囲の行を表すRangeオブジェクトを返します。