我对VBA还是很陌生,我有一个XLS模板,我试图在其中构建消息框,以便用户在模板允许之前在A列中的单元格不为空的情况下强制填写B列中的单元格保存。当前,无论单元格是否为空白,它都会弹出一条消息,一旦单击“确定”,它仍然会保存。有没有建议修改下面的代码以执行所需的操作?
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sub Check()
Dim rng As Range
Dim i As Long
'Set the range in column A you want to loop through
Set rng = Range("A1:A10000")
For Each cell In rng
'test if cell is empty
If cell.Value <> "" Then
MsgBox "Cell in column B requires user input in the format of PH_PSIxxxxxx"
Cancel = True
End If
Next
End Sub