A列 B列 C列 D列
↓ 1行目・・・ブランク行
↓ 2行目・・・ブランク行
↓ 3行目・・・ブランク行
商品 単価 数量 備考
C5セルから順に下へ数量を足していき、inputboxで指定した合計数
になったら、その対象セルがある行を選択状態にしたい。
丁度指定した合計数にならない場合は、プラスマイナスでその数に近い行を選択。
下記は合計数を4000で指定しているケースですが、毎回指定する
合計数が違うため、合計枚数をInputBoxで指定したいのですが、
どうすればよいでしょうか?
Sub Macro()
Dim r As Long
Dim count As Long
Dim LastRow As Long
'合計の数を指定
Const CountMax As Long = 4000
LastRow = Range("C65536").End(xlUp).Row
For r = 5 To LastRow
count = count + Cells(r, 3).Value
If count >= CountMax Then
Exit For
End If
Next r
If count - CountMax > CountMax - (count - Cells(r - 1, 3).Value) Then
r = r - 1
End If
Rows(r).Select
End Sub
宜しくお願い致します。
Sub Macro() Dim r As Long Dim count As Long Dim LastRow As Long Dim CountMax As Long CountMax = Application.InputBox(Prompt:="数字を指定してください。", Type:=1) If CountMax = False Then Exit Sub LastRow = Range("C65536").End(xlUp).Row For r = 5 To LastRow count = count + Cells(r, 3).Value If count >= CountMax Then Exit For End If Next r If count - CountMax > CountMax - (count - Cells(r - 1, 3).Value) Then r = r - 1 End If Rows(r).Select End Sub
参考