必填字段为红色。现在如何保存?

时间:2016-04-27 18:28:16

标签: excel vba excel-vba

它的作用:需要用户的字段。如果缺少特定字段,则阻止用户保存。将这些字段变为红色,直到正确保存。

我需要什么:嗯,我该怎么想保存这个......

我想要的是:由于工作表是空白的。我救不了。和必填字段为红色。即使我能保存,我也希望细胞不会被填满,直到我把它推出去。

View Original Post Here

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim xlSht As Worksheet
Dim cellsNotPopulated As Boolean
cellsNotPopulated = False

Set xlSht = ThisWorkbook.Worksheets("1st Call")

    With xlSht

        If .Range("F7") = "" Then
            .Range("F7").Interior.Color = RGB(255, 0, 0)
            cellsNotPopulated = True
        Else
            .Range("F7").Interior.ColorIndex = xlNone
        End If
    End With

    If cellsNotPopulated = True Then
        MsgBox "Please review the highlighted cells and ensure the fields are populated."
        Cancel = True
    End If
End Sub

2 个答案:

答案 0 :(得分:4)

如果您正处于开发阶段并希望“违反规则”并保存您当前的工作,那么请在标准模块中使用:

Sub MyPrivateSave()
    Application.EnableEvents = False
    ThisWorkbook.Save
    Application.EnableEvents = True
End Sub

当然,完成开发后,在将工作簿发送给用户之前,您将删除这个小“保存工具”。

答案 1 :(得分:1)

或添加为第一行if environ("Username")=your username then exit sub