Making fields mandatory of a specific sheet on workbook save

前端 未结 3 1542
忘了有多久
忘了有多久 2021-01-03 12:39

I am using macros in excel to make fields mandatory in excel workbook. However, the problem is that the workbook contains multiple worksheets and the macro applies to all th

3条回答
  •  [愿得一人]
    2021-01-03 13:18

    Please try:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      For i = 1 To ThisWorkbook.Sheets.Count 
       If ThisWorkbook.Sheets(i).Name = "Name of specific sheet" Then
         If ThisWorkbook.Sheets(i).Cells(5, 2).Value = "" Then
            MsgBox "Please fill cell B5"
            Cancel = True
         End If
       End If
      Next i
    End Sub
    

    Where the "Name of specific sheet" is the worksheet name that you validate Cells(5, 2).Value = "" for.

提交回复
热议问题