我已尝试使用此代码创建必填字段,但问题是它在转到单元格之前显示错误消息。
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
答案 0 :(得分:1)
此代码
代码
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 :(得分:0)
另一种方法是:
Range("A1").Interior.Color = RGB(255, 0, 0) 'red
Range("A1").Interior.Color = RGB(0, 255, 0) 'green
答案 2 :(得分:-1)
我认为您使用的事件可能会导致您的问题。
您可以使用Workbook_BeforeSave
事件实现必填字段。一旦用户尝试保存excel文件,这将被解雇。现在,检查您定义为必需的所有字段,并显示相应的“错误消息”。
当您设置Cancel = True
时,您可以中止保存过程,然后真正强制用户在必填字段中添加内容。
在Workbook_BeforeSave
活动中,您可以粘贴已经实施的所有支票。
有关Workbook_BeforeSave
的更多详细信息,请访问:https://msdn.microsoft.com/de-de/library/office/ff840057.aspx
您可以在此处找到有关如何实施Workbook_BeforeSave
活动的更多信息:http://www.positivevision.biz/blog/bid/153139/Excel-Tips-and-Tricks-Mandatory-Cell-Input