|
本章で解説するセルアクセス方法を用いた
「高校数学からはじめるディープラーニング」および 「Excel / OpenOffice で学ぶフーリエ変換入門」では マクロをソース閲覧可能な形で用いています。 |
Option Explicit
Sub Main()
Dim x1 As Double, x2 As Double
Dim n As Long, nd As Long
Dim x As Double, y As Double, dx As Double
x1 = -3.14
x2 = 3.14
nd = 30000
dx = (x2 - x1) / nd
ThisComponent.addActionLock() ' 画面更新抑制
For n = 0 To nd - 1 ' nd 個のデータを書き込む
x = x1 + dx * n
y = Sin(x)
ThisComponent.Sheets(0).getCellByPosition(0, n).Value = x
ThisComponent.Sheets(0).getCellByPosition(1, n).Value = y
Next n
ThisComponent.removeActionLock() ' 画面更新抑制解除
End Sub
|
| ActionLock の設定なし (画面更新有効) | 約 90 秒 |
| ActionLock の設定あり (画面更新無効) | 約 31 秒 |
Option Explicit
Sub Main()
Dim x1 As Double, x2 As Double
Dim n As Long, nd As Long
Dim x As Double, y As Double, dx As Double
x1 = -3.14
x2 = 3.14
nd = 30000
dx = (x2 - x1) / nd
Dim celldata() As Variant
ReDim celldata(nd - 1) ' 0...nd-1 で定義された配列を確保
For n = 0 To nd - 1 ' nd 個のデータを書き込む
x = x1 + dx * n
y = Sin(x)
celldata(n) = Array(x, y) ' 二列分のデータを Array() でくくって全て配列に格納
' 一列分のデータであっても Array() でくくる必要あり
Next n
ThisComponent.addActionLock()
ThisComponent.Sheets(0).getCellRangeByPosition(0, 0, 1, nd - 1).setDataArray(celldata)
' 実際のセルへの書き込み
ThisComponent.removeActionLock()
Erase celldata ' 確保した配列を解放
End Sub
|
| 画面更新有効: 230 [秒] |
| 画面更新無効: 44 [秒] |
| 配列丸ごと書き込み: 1 [秒] |
Option Explicit
Sub Main()
Dim x1 As Double, x2 As Double
Dim n As Long, nd As Long
Dim x As Double, y As Double, dx As Double
Dim t1 As Double, t2 As Double
x1 = -3.14
x2 = 3.14
nd = 30000
dx = (x2 - x1) / nd
Dim celldata() As Variant
ReDim celldata(nd - 1) ' 0...nd-1 で定義された配列を確保
For n = 0 To nd - 1 ' nd 個のデータを書き込む
x = x1 + dx * n
y = Sin(x)
celldata(n) = Array(x, y) ' 二列分のデータを Array() でくくって全て配列に格納
' 一列分のデータであっても Array() でくくる必要あり
Next n
Dim celldata_row() ' データを celldata() から読み取るための配列
t1 = Timer
For n = 0 To nd - 1 ' nd 個のデータを書き込む
celldata_row = celldata(n)
ThisComponent.Sheets(0).getCellByPosition(0, n).Value = celldata_row(0)
ThisComponent.Sheets(0).getCellByPosition(1, n).Value = celldata_row(1)
Next n
t2 = Timer
MsgBox ("画面更新有効: " & t2 - t1 & " [秒]")
ThisComponent.addActionLock() ' 画面更新抑制
t1 = Timer
For n = 0 To nd - 1 ' nd 個のデータを書き込む
celldata_row = celldata(n)
ThisComponent.Sheets(0).getCellByPosition(0, n).Value = celldata_row(0)
ThisComponent.Sheets(0).getCellByPosition(1, n).Value = celldata_row(1)
Next n
t2 = Timer
ThisComponent.removeActionLock() ' 画面更新抑制解除
MsgBox ("画面更新無効: " & t2 - t1 & " [秒]")
ThisComponent.addActionLock() ' 画面更新抑制
t1 = Timer
ThisComponent.Sheets(0).getCellRangeByPosition(0, 0, 1, nd - 1).setDataArray(celldata)
t2 = Timer
ThisComponent.removeActionLock() ' 画面更新抑制解除
MsgBox ("配列丸ごと書き込み: " & t2 - t1 & " [秒]")
Erase celldata ' 確保した配列を解放
End Sub
|
Dim celldata() Dim celldata_row() celldata() = ThisComponent.Sheets(0).getCellRangeByPosition(0, 0, 2, 1).getDataArray() celldata_row()=celldata(0) ' 1行目が celldata_row(0), celldata_row(1), celldata_row(2) として読み取れる celldata_row()=celldata(1) ' 2行目が celldata_row(0), celldata_row(1), celldata_row(2) として読み取れる |