「Excel / OpenOffice で学ぶフーリエ変換入門」では本章で解説するセルアクセス方法を用いたマクロを
ソース閲覧可能な形で収録しております。 |
Sub prog11_5() fillfunc -3.14, 3.14, 50000 End Sub Sub fillfunc(x1 As Double, x2 As Double, nd As Long) Dim n As Long Dim x As Double, y As Double, dx As Double dx = (x2 - x1) / nd Application.ScreenUpdating = False ' 画面更新抑制 For n = 0 To nd - 1 ' nd 個のデータを書き込む x = x1 + dx * n y = Sin(x) Sheet1.Cells(n + 1, 1).Value = x Sheet1.Cells(n + 1, 2).Value = y Next n Application.ScreenUpdating = True ' 画面更新抑制解除 End Sub |
ScreenUpdating の設定なし (画面更新有効) | 2.671875[秒] |
ScreenUpdating の設定あり (画面更新無効) | 2.546875[秒] |
Sub prog11_5() fillfunc -3.14, 3.14, 50000 End Sub Sub fillfunc(x1 As Double, x2 As Double, nd As Long) Dim n As Long Dim x As Double, y As Double, dx As Double Dim celldata() As Variant ReDim celldata(nd - 1, 1) ' 0...nd-1 および 0...1 で定義された二次元配列を確保 dx = (x2 - x1) / nd For n = 0 To nd - 1 ' nd 個のデータを書き込む x = x1 + dx * n y = Sin(x) celldata(n, 0) = x ' データをあらかじめ二次元配列に全て格納 celldata(n, 1) = y ' 配列の添字は 0 から用いていることに注意 Next n Application.ScreenUpdating = False Range(Cells(1, 1), Cells(nd, 2)) = celldata ' 実際のセルへの書き込み Application.ScreenUpdating = True Erase celldata ' 確保した二次元配列を解放 End Sub |
画面更新有効: 2.59375[秒] |
画面更新無効: 2.53125[秒] |
配列丸ごと書き込み: 0.0859375[秒] |
Sub prog11_5() fillfunc -3.14, 3.14, 50000 End Sub Sub fillfunc(x1 As Double, x2 As Double, nd As Long) Dim n As Long Dim x As Double, y As Double, dx As Double Dim t1 As Double, t2 As Double Dim celldata() As Variant ReDim celldata(nd - 1, 1) ' 0...nd-1 および 0...1 で定義された二次元配列 dx = (x2 - x1) / nd For n = 0 To nd - 1 ' nd 個のデータを書き込む x = x1 + dx * n y = Sin(x) celldata(n, 0) = x celldata(n, 1) = y Next n t1 = Timer For n = 1 To nd Sheet1.Cells(n, 1).Value = celldata(n - 1, 0) Sheet1.Cells(n, 2).Value = celldata(n - 1, 1) Next n t2 = Timer MsgBox ("画面更新有効: " & t2 - t1 & " [秒]") Application.ScreenUpdating = False t1 = Timer For n = 1 To nd Sheet1.Cells(n, 1).Value = celldata(n - 1, 0) Sheet1.Cells(n, 2).Value = celldata(n - 1, 1) Next n t2 = Timer Application.ScreenUpdating = True MsgBox ("画面更新無効: " & t2 - t1 & " [秒]") Application.ScreenUpdating = False t1 = Timer Range(Cells(1, 1), Cells(nd, 2)) = celldata t2 = Timer Application.ScreenUpdating = True MsgBox ("配列丸ごと書き込み: " & t2 - t1 & " [秒]") End Sub |
Dim celldata As Variant celldata = Range(Cells(1, 1), Cells(2, 3)).Value ' データが配列に以下のように格納される ' celldata(1,1), celldata(1,2), celldata(1,3) ' celldata(2,1), celldata(2,2), celldata(3,3) |