使用vba在excel中强制使用单元格

我已经试过这个代码来创build必填字段,但问题是它是在去单元格之前显示错误消息。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.count, "A").End(xlUp).Row Dim I, J As Integer For I = 1 To lastRow If Cells(I, "C").Value = "" Then MsgBox "Please Enter Business Type Value", vbOKOnly Exit Sub End If 'If Cells(I, "D").Value = "" Then 'MsgBox "Please Enter Customer Account Code", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "E").Value = "" Then 'MsgBox "Please Enter Transport Mode Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "F").Value = "" Then 'MsgBox "Please Enter Incoterm Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "K").Value = "" Then 'MsgBox "Please Enter From date Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "L").Value = "" Then 'MsgBox "Please Enter To date Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "K").Value > Cells(I, "L").Value Then 'MsgBox "To date value should greater than From value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "N").Value = "" Then 'MsgBox "Please Enter Origin Country Code Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "O").Value = "" Then 'MsgBox "Please Enter Point of Origin Location Code Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "R").Value = "" Then 'MsgBox "Please Enter Port of Loading Code Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "S").Value = "" Then 'MsgBox "Please Enter Origin Clearance Location Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "T").Value = "" Then 'MsgBox "Please Enter Destination Clearance Location Value", vbOKOnly 'Exit Sub 'End If 'If Cells(I, "U").Value = "" Then 'MsgBox "Please Enter Port of Discharge Code Value", vbOKOnly 'Exit Sub 'End If If Cells(I, "Y").Value = "" Then MsgBox "Please Enter Consignee Final Destination Location Code Value", vbOKOnly Exit Sub End If If Cells(I, "Z").Value = "" Then MsgBox "Please Enter Destination Country Code Value", vbOKOnly Exit Sub End If If Cells(I, "AF").Value = "" Then MsgBox "Please Enter Active status Value", vbOKOnly Exit Sub End If If Cells(I, "AH").Value = "" Then MsgBox "Please Enter Carrier Allocation Number Value", vbOKOnly Exit Sub End If If Cells(I, "AI").Value = "" Then MsgBox "Please Enter Carrier Allocation Valid From Date Value", vbOKOnly Exit Sub End If If Cells(I, "AJ").Value = "" Then MsgBox "Please Enter Carrier Nomination Sequence Number Value", vbOKOnly Exit Sub End If Next I End With End Sub 

这个代码

  • 跟踪A1:A10看是否有任何细胞被改变
  • 然后查看Y行中的对应单元是否为空
    • 如果为空,则返回消息,然后将A1:A10中已更改的单元清空

 Private Sub Worksheet_Change(ByVal Target As Range) Dim rng1 As Range Dim rng2 As Range Set rng1 = Intersect(Target, [a1:a10]) 'exit if no cells in A1:A10 are changed If rng1 Is Nothing Then Exit Sub 'turn off events to avoid code retriggering itself Application.EnableEvents = False For Each rng2 In rng1 If Cells(rng2.Row, "Y") = vbNullString Then MsgBox "Please Enter Consignee Final Destination Location Code Value, your entry will be deleted", vbOKOnly rng2.Value = vbNullString End If Next Application.EnableEvents = True End Sub 

另一种方法是:

  1. 在您的Worksheet_SelectionChange使所有不通过您的检查红色的单元格。 所有谁通过你的支票绿色。 你可以用下面的代码来做到这一点。 不要在这个过程中产生任何错误信息。

Range("A1").Interior.Color = RGB(255, 0, 0) 'red

Range("A1").Interior.Color = RGB(0, 255, 0) 'green

  1. 如果所有的检查都通过了,只允许保存工作簿。 因此,你可以使用我在另一个答案中提供给你的过程。

我认为你正在使用的事件可能会导致你的问题。

您可以使用Workbook_BeforeSave事件实现必填字段。 一旦用户试图保存excel文件,这将被解雇。 现在您检查您定义为强制性的所有字段,并显示相应的“错误消息”。

当你设置Cancel = True你可以中止保存过程,然后强制用户在必填字段中添加一些内容。

Workbook_BeforeSave事件中,您可以粘贴您已经实施的所有检查。

有关Workbook_BeforeSave更多详细信息,请访问: https : //msdn.microsoft.com/de-de/library/office/ff840057.aspx

在这里你可以find更多关于如何实现Workbook_BeforeSave事件的信息: http : //www.positivevision.biz/blog/bid/153139/Excel-Tips-and-Tricks-Mandatory-Cell-Input