戻り原料使用登録

Dim ws As Worksheet
Dim searchValue As String
Dim updateValue As String
Dim useDate As String
Dim searchRange As Range
Dim found As Range
Dim lastUpdatedCell As Range
Dim dateInput As String
Dim userChoice As Integer

‘ シートを設定
Set ws = ThisWorkbook.Sheets(“Sheet1”) ‘ シート名を変更してください

‘ 初期使用日を本日に設定
useDate = Format(Date, “yyyy/mm/dd”)

Do
‘ ユーザーにG2の値を入力させる
searchValue = InputBox(“E列で検索する値を入力してください(終了するにはキャンセルを押してください)”, “検索値入力”)
If searchValue = “” Then Exit Sub ‘ キャンセルまたは空白の場合は終了

‘ ユーザーにG3の値を入力させる
updateValue = InputBox(“一致した行のG列に記入する値を入力してください”, “更新値入力”)
If updateValue = “” Then Exit Sub ‘ キャンセルまたは空白の場合は終了

‘ 使用日を選択
userChoice = MsgBox(“任意の日付を入力しますか? [はい] 任意の日付を入力 | [いいえ] 本日の日付を使用”, vbYesNo + vbQuestion, “使用日選択”)

If userChoice = vbYes Then
‘ 使用日を入力(空白なら変更なし)
dateInput = InputBox(“使用日を入力してください(yyyy/mm/dd形式)。空白の場合は” & useDate & “が使用されます。”, “使用日入力”)
If dateInput <> “” Then
useDate = dateInput ‘ 入力があれば使用日を更新
End If
Else
useDate = Format(Date, “yyyy/mm/dd”) ‘ 本日の日付にリセット
End If

‘ E列の検索範囲を設定
Set searchRange = ws.Range(“E:E”)

‘ 検索
Set found = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)

If Not found Is Nothing Then
firstAddress = found.Address
Do
‘ G列を更新
ws.Cells(found.Row, “G”).Value = updateValue

‘ F列に使用日を記入
ws.Cells(found.Row, “F”).Value = useDate

‘ 最後に更新したセルを記録
Set lastUpdatedCell = ws.Cells(found.Row, “G”)

‘ 次を検索
Set found = searchRange.FindNext(found)
Loop While Not found Is Nothing And found.Address <> firstAddress
End If

‘ 最後に更新したセルをアクティブセルに設定
If Not lastUpdatedCell Is Nothing Then
lastUpdatedCell.Select
End If

Loop ‘ ループして次の入力を待つ

棚卸用

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow As Long
Dim destRow As Long
Dim i As Long

‘ シートを設定
Set ws1 = ThisWorkbook.Sheets(“Sheet1”)
Set ws2 = ThisWorkbook.Sheets(“Sheet2”)

‘ シート1の最終行を取得
lastRow = ws1.Cells(ws1.Rows.Count, “G”).End(xlUp).Row
destRow = 5 ‘ 貼り付け開始行を5行目に設定

‘ G列の値を確認し、0の行をシート2にコピー
For i = 1 To lastRow
If ws1.Cells(i, “G”).Value = 0 Then
ws1.Rows(i).Copy Destination:=ws2.Rows(destRow)
destRow = destRow + 1
End If
Next i