こちらに「条件分岐+セル操作+メール送信(Outlook連携)」の実務サンプルをまとめました。
セルの値を判定して色付けし、条件を満たした場合にOutlookでメールを自動送信する流れです。
サンプル1:セルの値に応じて色付け+メール送信
Sub CheckAndMail()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim outlookApp As Object, mailItem As Object
Dim msgBody As String
Set ws = ThisWorkbook.Sheets("Data")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Outlook起動
Set outlookApp = CreateObject("Outlook.Application")
For i = 2 To lastRow
If ws.Cells(i, 2).Value >= 100000 Then
' 条件を満たすセルを色付け
ws.Rows(i).Interior.Color = RGB(255, 200, 200)
' メール作成
Set mailItem = outlookApp.CreateItem(0)
msgBody = "売上高が基準を超えました。" & vbCrLf & _
"顧客名:" & ws.Cells(i, 1).Value & vbCrLf & _
"売上:" & ws.Cells(i, 2).Value
With mailItem
.To = "example@company.com"
.Subject = "売上アラート通知"
.Body = msgBody
.Send
End With
End If
Next i
End Sub
VB👉 売上が10万円以上なら行を赤くし、通知メールを送信。
サンプル2:在庫不足を検知して担当者にメール
Sub StockAlert()
Dim ws As Worksheet
Dim outlookApp As Object, mailItem As Object
Dim i As Long, lastRow As Long
Set ws = Sheets("Stock")
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Set outlookApp = CreateObject("Outlook.Application")
For i = 2 To lastRow
If ws.Cells(i, 2).Value < 10 Then
ws.Cells(i, 2).Interior.Color = RGB(255, 99, 71) ' 赤
Set mailItem = outlookApp.CreateItem(0)
With mailItem
.To = "stock@company.com"
.Subject = "在庫不足アラート"
.Body = "商品:" & ws.Cells(i, 1).Value & vbCrLf & _
"在庫数:" & ws.Cells(i, 2).Value & vbCrLf & _
"至急補充をお願いします。"
.Send
End With
End If
Next i
End Sub
VB👉 在庫数が10未満ならセルを赤くし、在庫担当者にメール通知。
サンプル3:Worksheet_Changeイベントで即時通知
Private Sub Worksheet_Change(ByVal Target As Range)
Dim outlookApp As Object, mailItem As Object
If Target.Column = 3 And IsNumeric(Target.Value) Then
If Target.Value > 200 Then
Target.Interior.Color = RGB(255, 182, 193)
Set outlookApp = CreateObject("Outlook.Application")
Set mailItem = outlookApp.CreateItem(0)
With mailItem
.To = "alert@company.com"
.Subject = "セル値超過通知"
.Body = "セル " & Target.Address & " の値が200を超えました。" & vbCrLf & _
"値:" & Target.Value
.Send
End With
End If
End If
End Sub
VB👉 セルに入力された値が200を超えた瞬間に色付け+メール送信。
✅ まとめ
- 条件分岐でしきい値を判定
- セル操作で色付けやマーキング
- Outlook連携で自動通知メール送信
💡 この仕組みを応用すれば、売上アラート・在庫不足通知・進捗遅延のリマインドなど、業務の自動化に直結します。
