请求实现事件触发时通过MsgBox告知触发的Block信息
Got it, let's fix this so your MsgBox tells you exactly which block is ending soon! The core idea is to tie each block's unique identifier (like its name/number) to the time-checking logic, then pass that info to your notification message.
Step 1: Track Block Details
First, make sure you have a way to store each block's key info—name, total duration, and elapsed time. If you're using VBA (since you mentioned MsgBox), here's a simple way with a custom type:
' Define a structure to hold each block's details Type BlockInfo BlockName As String TotalDuration As Double ' Total time in minutes ElapsedTime As Double ' Time already passed in minutes End Type ' Array to store all your blocks Dim Blocks() As BlockInfo
Step 2: Initialize Your Blocks
Set up your blocks with their names and durations (adjust this to match your actual blocks):
Sub SetupBlocks() ReDim Blocks(1 To 2) ' Example: 2 blocks Blocks(1).BlockName = "Block 1" Blocks(1).TotalDuration = 60 ' 1 hour total Blocks(2).BlockName = "Block 2" Blocks(2).TotalDuration = 45 ' 45 minutes total End Sub
Step 3: Check Time Conditions & Trigger Notifications
Create a function that checks each block's remaining time. When the 15-minute mark hits, pass the block's name to a notification function:
Sub MonitorBlockTimes() Dim i As Integer Dim remainingTime As Double For i = LBound(Blocks) To UBound(Blocks) ' Calculate remaining time (update ElapsedTime with your real-time logic) remainingTime = Blocks(i).TotalDuration - Blocks(i).ElapsedTime ' Trigger alert when exactly 15 mins remain If remainingTime = 15 Then ShowBlockAlert Blocks(i).BlockName, remainingTime End If Next i End Sub
Step 4: Build the Custom MsgBox
This function takes the block name and remaining time, then generates the exact message you want:
Sub ShowBlockAlert(blockName As String, remainingMins As Double) Dim alertMsg As String alertMsg = blockName & " ends in " & remainingMins & " mins" MsgBox alertMsg, vbInformation, "Time Alert" End Sub
If You're Using Excel to Track Blocks
If your blocks are stored in an Excel sheet (e.g., names in column A, durations in B, elapsed time in C), adjust the monitoring function like this:
Sub CheckBlocksFromSheet() Dim lastRow As Integer Dim i As Integer Dim blockName As String Dim remainingTime As Double lastRow = ThisWorkbook.Sheets("BlockTracker").Cells(Rows.Count, "A").End(xlUp).Row ' Loop through rows (skip header row if you have one) For i = 2 To lastRow blockName = ThisWorkbook.Sheets("BlockTracker").Cells(i, "A").Value remainingTime = ThisWorkbook.Sheets("BlockTracker").Cells(i, "B").Value - ThisWorkbook.Sheets("BlockTracker").Cells(i, "C").Value If remainingTime = 15 Then ShowBlockAlert blockName, remainingTime End If Next i End Sub
Key Takeaways
- Always associate each block with a clear identifier (name/number) so you can reference it later.
- Pass that identifier along with the time condition to your notification logic—don't hardcode block names!
- Adjust the
ElapsedTimeupdate logic to match how you're tracking time for each block (e.g., using a timer, manual updates, etc.)
内容的提问来源于stack exchange,提问作者Jackie Chua

