使用vba在excel中强制单元格

时间:2015-12-16 11:15:14

标签: excel excel-vba vba

我已尝试使用此代码创建必填字段,但问题是它在转到单元格之前显示错误消息。

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

3 个答案:

答案 0 :(得分:1)

此代码

  • 跟踪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 :(得分:0)

另一种方法是:

  1. 在您的Worksheet_SelectionChange中,将所有未通过检查的单元格设为红色。所有通过绿色支票的人。您可以使用以下代码执行此操作。请勿在此过程中生成任何错误消息。
  2. Range("A1").Interior.Color = RGB(255, 0, 0) 'red

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

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

答案 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