[英]Type Mismatch 13 Error when deleting from a merged cell - VBA
當我在合並的單元格上按刪除按鈕時出現錯誤(Type Mismatch 13),但是,此錯誤僅在合並單元格時顯示,否則不會引發錯誤,我不明白為什么,請按照代碼進行操作:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("AG3") Then
If Target.Value = 5 Then
Sheets("Sheet6").CommandButton6.Visible = False
Else
Sheets("Sheet6").CommandButton6.Visible = True
End If
End If
On Error Resume Next
Sheets("Sheet6").CommandButton6.Visible = False
On Error GoTo 0
End Sub
什么東西少了?
單元格AG3
的數字從 0 到 5 不等
當您從合並的單元格中刪除一個值時, Target
會獲取整個范圍,而不僅僅是第一個單元格。 此行為不同於將值添加到合並單元格時的行為(在這種情況下,只有合並區域中的第一個單元格被傳入)。
例如 - 如果我將工作表上的D2:D8
與此事件處理程序合並:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address, TypeName(Target.Value)
End Sub
在合並的單元格中輸入“測試”會給我:
$D$2 String
並刪除該值給我:
$D$2:$D$8 Variant()
我才剛剛知道。
因此,請考慮查看Target.Cells(1)
而不是Target
(取決於您的確切用例)
Target
可以是任意數量的單元格。 因此,最好獲得它與所需單元格的交集,如果它們相交,則繼續此交集(實際上是所需單元格)。Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim iCell As Range: Set iCell = Intersect(Range("AG3"), Target)
If iCell Is Nothing Then Exit Sub
If iCell.Value = 5 Then
Sheets("Sheet6").CommandButton6.Visible = True
Else
Sheets("Sheet6").CommandButton6.Visible = False
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.