引用范围的targe.value时如何避免类型不匹配错误?

时间:2019-05-03 19:30:32

标签: excel vba

我是VBA的新手,希望在下面的代码中提供帮助。

调试器在If target.Value <> "" Then(底部子句的第一行)上停止,我怀疑这是由于目标是一个可能具有多个目标值的范围的事实。这种思路正确吗?如果是这样,我该如何解决这个问题。

注意:“ FILLABLE_TOP_REG”,“ CHK_PROD”和“ FILLABLE_BOT”在我的工作簿的sheet1上被命名为范围。

谢谢!

如果target.cells.count = 1和target.value <>“” ... 对于目标中的每个单元格...

Public Sub Worksheet_SelectionChange(ByVal target As Range)

Dim TopREG_Range As Range

'PROMPTS USER TO CONFIRM CHANGE/DELETE
If Not Intersect(target, Range("FILLABLE_TOP_REG")) Is Nothing Then
    Call warning_msg(Range("H7"), target)
End If
If Not Intersect(target, Range("CHK_PROD")) Is Nothing Then
    Call warning_msg(Range("H7"), target)
End If
If Not Intersect(target, Range("FILLABLE_BOT")) Is Nothing Then
    Call warning_msg(Range("H" & target.Row), target)
End If
End Sub

Public Sub warning_msg(redirect As Range, target As Range)

If target.Value <> "" Then
            Dim Msg As String
                Msg = "Are you sure you want to delete and/or modify the following cell?" & vbCrLf & vbCrLf & Selection.Address & "= " & Selection.Text
            Dim Ans As VbMsgBoxResult
                Ans = MsgBox(Msg, vbYesNo + vbCritical)
                Select Case Ans
            Case vbYes: Selection.ClearContents
            Case vbNo: redirect.Select
    End Select
  End If
End Sub

0 个答案:

没有答案