Attribute VB_Name = "modUtils"
Option Explicit

' 取工作表指定列 prefix+三位流水的下一个编号（L019、B008、D007……）
Public Function NextId(ws As Worksheet, col As Long, prefix As String) As String
    Dim r As Long, lastRow As Long, maxNo As Long
    lastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
    For r = 2 To lastRow
        If Val(Mid(CStr(ws.Cells(r, col).Value), Len(prefix) + 1)) > maxNo Then
            maxNo = Val(Mid(CStr(ws.Cells(r, col).Value), Len(prefix) + 1))
        End If
    Next r
    NextId = prefix & Format(maxNo + 1, "000")
End Function

' 取指定列的下一个空行（从第2行起算）
Public Function NextRow(ws As Worksheet, col As Long) As Long
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
    If lastRow < 2 Then lastRow = 1
    NextRow = lastRow + 1
End Function

Public Function Sh(name As String) As Worksheet
    Set Sh = ThisWorkbook.Worksheets(name)
End Function

' 统一错误提示，避免宏半途失败却没有反馈
Public Sub Fail(where As String)
    MsgBox where & " 执行失败：" & Err.Description & "，未完成的写入请人工检查。", vbExclamation
End Sub
