Hi, I am still try to figuring out about this On Error Statement. I have a case in Debra Dalgleish post (http://www.contextures.com/xlDataVal10.html) about "Excel Data Validation Combo Box".
Here the code:
The code with red higlight is my question.
Thanks...
Here the code:
Code:
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'Hide combo box and move to next cell on Enter and Tab
Select Case KeyCode
Case 9
ActiveCell.Offset(0, 1).Activate
Case 13
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
Cancel = True
End If
Set cboTemp = ws.OLEObjects("TempCombo")
[COLOR=#ff0000] On Error Resume Next[/COLOR]
With cboTemp
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
[COLOR=#ff0000]On Error GoTo errHandler[/COLOR]
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 15
.Height = Target.Height + 5
.ListFillRange = ws.Range(str).Address
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
[COLOR=#ff0000]errHandler:
Application.EnableEvents = True
Exit Sub[/COLOR]
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
[COLOR=#ff0000] On Error Resume Next[/COLOR]
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
End If
[COLOR=#ff0000]errHandler:
Application.EnableEvents = True
Exit Sub[/COLOR]
End Sub
- Why the author using On Error Resume Next? Because I don't get error if I delete this statement
- Why on errHandler: using Application.EnableEvents = True?
- In the Private Sub Worksheet_SelectionChange(ByVal Target As Range), if I delete this statement (code below) no error occur. Why the author still using?:
Code:
[COLOR=#ff0000]errHandler:
Application.EnableEvents = True
Exit Sub[/COLOR]