工作表更改代码出现 VBA 数据验证错误

问题描述 投票:0回答:1

我一直在增强一个输入表,用户将通过某些列中的下拉菜单进行各种选择,并在其他列中手动输入值。相关错误发生在手动输入单元格中(G 列到第 n 列)。

对于其他上下文,我实现了各种工作表更改编码,其中包括通过 VBA 进行数据验证。


问题

与此表关联的工作表更改代码之前可以正常工作,没有任何问题,但当我添加更多代码时,我现在正在处理在任何类型的手动输入后显示 VBA 输入的数据验证公式的问题,从而导致 DV如果/当尝试再次手动更改单元格时填充错误消息。

1) Empty cell/empty DV (as expected) 2) Filled cell/filled DV formula 3) DV error when trying to change cell value 4) DV formula maintained even after cell made blank

它有点庞大且重复,所以我分享一个片段,以便您可以了解它与其他列(目标)的外观。


当前代码(不起作用)

Option Explicit

'Upon ws activating/opening:
'---------------------------

Private Sub Worksheet_Activate()

    'Set to true before reading ws changes
    '---------------------------
    Call Enable_IP

    'WS name (for finalizing wb)
    '---------------------------
    With Range("A4")
        .Value = Me.Name
        .WrapText = False
        .Font.Color = vbWhite
    End With

End Sub


'All other ws changes:
'---------------------------

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rngB As Range
    Dim rngG As Range
    Dim rngH As Range
    Dim rngI As Range
    Dim rngJ As Range
    Dim myCell As Range
    
    Set rngB = Range("B7:B35")
    Set rngD = Range("D7:D35")
    Set rngG = Range("G7:G35")
    Set rngH = Range("H7:H35")
    Set rngI = Range("I7:I35")
    Set rngJ = Range("J7:J35")
    
    rngG.NumberFormat = "$#,##0.00"  
    rngH.NumberFormat = "0.00%"      
    rngI.NumberFormat = "$#,##0.00"  
    rngJ.NumberFormat = "$#,##0.00"  
    
    On Error GoTo ErrHandler 
       
    '---------------------------------------------------------------
    'Col B change event: null row if Col B empty/changed
    
    If Not Intersect(Target, rngB) Is Nothing Then
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        With Target
            'clear drop-downs
            Range(.Offset(0, 2), .Offset(0, 4)).ClearContents
            'clear # inputs
            Range(.Offset(0, 5), .Offset(0, 8)).ClearContents
              
            If .Value = "" Then
                Range(.Offset(0, 2), .Offset(0, 4)).ClearContents
                Range(.Offset(0, 5), .Offset(0, 8)).ClearContents
            End If
        End With
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End If
    
    '---------------------------------------------------------------
    'Col G as target: change events related to Col G changes
    
    If Not Intersect(Target, rngG) Is Nothing Then
            
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Select Case Target.Address
            
             Case "$G$7"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F7,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G7))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$8"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F8,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G8))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$9"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F9,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G9))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$10"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F10,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G10))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$11"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F11,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G11))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$12"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F12,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G12))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$13"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F13,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G13))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$14"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F14,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G14))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$15"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F15,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G15))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$16"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F16,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G16))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$17"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F17,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G17))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$18"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F18,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G18))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$19"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F19,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G19))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$20"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F20,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G20))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$21"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F21,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G21))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$22"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F22,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G22))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$23"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F23,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G23))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$24"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F24,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G24))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$25"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F25,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G25))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$26"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F26,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G26))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$27"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F27,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G27))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
            
            Case "$G$28"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F28,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G28))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$29"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F29,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G29))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$30"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F30,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G30))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$31"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F31,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G31))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$32"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F32,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G32))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$33"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F33,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G33))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$34"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F34,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G34))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case "$G$35"
                With Target.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F35,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G35))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With
                
            Case Else
        End Select
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End If
    
    '--------------------------------------------------------
    'Col H as target: change events related to Col H changes
            
    If Not Intersect(Target, rngH) Is Nothing Then
            
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Select Case Target.Address

........
            

我注意到,通过 VBA 更改单元格后,错误消息和 DV 公式不会显示在单元格中(我有一个附加功能,可以在特定情况下对某些单元格应用一些数学)。


早期代码(无 DV 错误/问题)

1) Empty cell/empty DV formula 2) Filled cell w/ no DV msg 3) Changed cell value w/ no DV msg or error

......

    'Col G as target: change events related to Col G changes
    
    If Not Intersect(Target, rngG) Is Nothing Then
            
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Select Case myCell.Address
            
             Case "$G$7"
                With rngG.Validation
                    .Delete
                    .Add Type:=xlValidateCustom, _
                    AlertStyle:=xlValidAlertStop, _
                    Operator:=xlBetween, _
                    Formula1:="IF(AND(XLOOKUP($F7,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25)),ISNUMBER(G7))=TRUE,TRUE,FALSE)"
                    .IgnoreBlank = True
                    .InCellDropdown = False
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = True
                End With

......
...
......
           Case Else

       End Select
  
       With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End If

................

总的来说,我觉得这个问题是我更新工作表更改代码的结果。我添加了额外的目标并稍微更改了语言,以避免我之前收到的其他小错误(之前没有收到 DV 错误)。

更新详情:

我之前在“Select Case myCell.Address”行遇到错误/停止,所以我更新了“target.Address”,然后这导致我还更改了“...rngG.Validation..”(并且其他范围)到 Target.Validation”,因为我在更改单元格的列的所有单元格中收到 DV 错误和消息。

除此之外,我刚刚添加/将添加一个或多个附加列作为目标范围。

如有任何帮助,我们将不胜感激!

excel vba validation onchange target
1个回答
0
投票

您的事件处理程序不处理

Target
的多单元格实例,因此请将其放在 Sub -

的顶部

If Target.CountLarge > 1 Then Exit Sub

您发布的代码的长度会阻碍人们对其进行审查。这对于

rngG
来说是等价的,而且更短:

    If Not Intersect(Target, rngG) Is Nothing Then
            
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        With Target.Validation
            .Delete
            .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, _
                 Operator:=xlBetween, _
Formula1:=Replace("=IF(AND(XLOOKUP($F<rw>,Merge1!$E$2#,XLOOKUP(G$6,Merge1!$F$1#,Merge1!$F$2:$V$25))," & _
                              "ISNUMBER(G<rw>))=TRUE,TRUE,FALSE)", "<rw>", Target.Row)
            .IgnoreBlank = True
            .InCellDropdown = False
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = False
            .ShowError = True
        End With
            
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End If
© www.soinside.com 2019 - 2024. All rights reserved.