例によってVBAのサイトを参考にしました。(総合練習問題2|VBA入門、https://excel-ubara.com/excelvba1/EXCELVBA326.html)
問題
A列の1から10行には0以上50以下の数があらかじめ入力されているとする。
それらの10個の数の中で 'display dialog' で入力した数以上の数の合計を求め、その結果を 'displa dialog' で表示する。
注意点
練習用データは適当に手入力で準備した。
apple script内でshell scriptを利用する 'do shell script' コマンドで、shell scriptの乱数を得るコマンド( 'jot' など)を使って練習用データを得るところからやることもできる。簡単にするため今回は行わず。
51以上の数を入力する、整数ではなく少数を入力するといった場合のエラー対応は今回は行わず。
(鳶嶋工房 / AppleScript / 入門 / 繰り返しで処理しよう、http://tonbi.jp/AppleScript/Introduction/08/) (鳶嶋工房 / AppleScript / 入門 / 変数は値を入れる袋、http://tonbi.jp/AppleScript/Introduction/06/) (jotコマンドの使い方: UNIX/Linuxの部屋、http://x68000.q-e-d.net/~68user/unix/pickup?jot)
set LF to " " tell application "Numbers" activate set tgtDoc to make new document tell tgtDoc tell sheet 1 delete every table set tgtTable to make new table with properties {row count:10, column count:10, header row count:0, header column count:0} tell tgtTable set height of every row to 36 set width of every column to 36 -- 以上がtableの準備 -- 練習用デ―タを準備 set myData to {0, 14, 34, 6, 9, 40, 41, 18, 5, 42} -- A列の1かな10行に練習用デ―タを入力 repeat with i from 1 to 10 set value of cell ("A" & i) to item i of myData end repeat -- display dialogで数値を入力する。 display dialog "0以上50以下の整数を入力してください。" default answer "" set myNum to text returned of result -- エラ―対応 try myNum as integer on error display dialog "文字列ではなく0以上50以下の整数を入力してください。" end try -- 指定した数以上の数の足し算 set sum to 0 as integer repeat with i from 1 to 10 if (value of cell ("A" & i) as integer) ≥ myNum then set sum to sum + (value of cell ("A" & i) as integer) end if end repeat -- display dialogで合計を表示する。 display dialog "A列の" & myNum & "以上の数の合計は" & sum & "です。" end tell end tell end tell end tell
Translated with DeepL
I referred to the VBA website here.
Qestion
Assume that the numbers between 0 and 50 are pre-populated in rows 1 to 10 of column A.
Find the sum of the numbers among those 10 numbers that are greater than or equal to the number entered in the 'display dialog' and display the result in the 'displa dialog'.
Notes.
The data for the exercise was prepared by hand as appropriate.
You can also use the 'do shell script' command to get random numbers in the shell script (such as 'jot') to get the practice data. For simplicity, we won't do that this time.
I didn't deal with errors when entering a number greater than 51, or when entering a small number instead of an integer.