LoginSignup
0
1

More than 5 years have passed since last update.

VBA sample Code

Posted at

VBAコード書きました。
※後ほど詳細共有予定です。

Sub ボタン1_Click()

'1.
B11 = Sheet1.Cells(11, 3).Value '日付
B12 = Sheet1.Cells(12, 3).Value '場所
B13 = Sheet1.Cells(13, 3).Value '田んぼ
B14 = Sheet1.Cells(14, 3).Value '水量
MaxCol = Sheet2.Range("C8").End(xlToRight).Column
MaxRow = Sheet2.Range("B8").End(xlDown).Row
firsttime = True '先頭行にB12,B13の同一データがない。/入力済に戻った時

'初期化時の行指定のため
If MaxRow = 1048576 Then
MaxRow = 8
End If
'http://excelvba.pc-users.net/fol7/7_1.html
lngYLine = Worksheets("Sheet1").Cells.Find(B13, LookAt:=xlWhole).Row
intXLine = Worksheets("Sheet1").Cells.Find(B13, LookAt:=xlWhole).Column
'' MsgBox B13 + "は" + CStr(lngYLine) + "行目の" _
'' + CStr(intXLine) + "列目にあります"
'' MsgBox (intXLine)
'' MsgBox (lngYLine)

    wtr = Cells(lngYLine, intXLine + 1).Value
    'MsgBox (wtr)

For i = 4 To MaxCol

' date
If Sheet2.Cells(8, i) = B11 Then
'MsgBox ("OK")

For l = 9 To MaxRow + 1

'
If Sheet2.Cells(l, 2).Value = B12 And Sheet2.Cells(l, 3).Value = B13 Then
Sheet2.Cells(l, i) = wtr / B14
firsttime = False 'B12,B13との同一データを見つけて入力済
End If

last_Data = l 'L
If Sheet2.Cells(l, 2).Value <> B12 Or Sheet2.Cells(l, 3).Value <> B13 Then '片方どちらかが違うととお

If MaxRow = last_Data Then

If firsttime <> False Then

' MsgBox ("!!")
Sheet2.Cells(MaxRow + 1, 2) = B12
Sheet2.Cells(MaxRow + 1, 3) = B13
Sheet2.Cells(MaxRow + 1, i) = wtr / B14
firsttime = False
End If
End If
End If

Next l

End If

Next i

firsttime = True

Dim varCheck As Variant

varDeleteCheck = Sheet1.CheckBox1.Value
varMoveCheck = Sheet1.CheckBox2.Value

'3 データ削除
If varDeleteCheck = True Then
Sheet1.Range("C11:C14").ClearContents
End If

'4 作業完了
If varMoveCheck = True Then
Sheet2.Activate
End If

End Sub

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1