SOLVED

Applying If Else logic to restrict VB function the area within Xcel sheet

Copper Contributor

Hi All, 

 

I am learning VBA and tried everything to fix the issue. Hoping best i would get the solution here.

 

Below is my VBA code. 

I have to restrict the area where this code will work(display a tick mark on double click & dissapear if again double clicked). Area should be defined as Column>7&&Column<32&&Row>4. 

=====================================================

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

 

If (Target.Column > 7 And Target.Column < 32) And Target.Value <> Chr(214) Then
With Target
.Value = Chr(214) 'Required
.Font.Name = "Symbol" 'Required
.Font.Size = 13
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom

End With

ElseIf (Target.Column > 7 And Target.Column < 32 And Target.Rows > 1 And Target.Rows < 3) And Target.Value = Chr(214) Then
Target.ClearContents

End If
End Sub

 

File is attached here. I have to save it as xls as xlsm format is not allowed to upload.

 

2 Replies
best response confirmed by Abhinav Gupta (Copper Contributor)
Solution

Does this work for you:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Intersect(Target, Me.Range("H5:DH1000")) Is Nothing Then Exit Sub
    If Target.Value <> Chr(214) Then
        With Target
            .Value = Chr(214)    'Required
            .Font.Name = "Symbol"    'Required
            .Font.Size = 13
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
        End With
        Cancel = True
    ElseIf Target.Value = Chr(214) Then
        Target.ClearContents
        Cancel = True
    End If
End Sub

Thanks so much. That worked. @Jan Karel Pieterse. You are awesome.
1 best response

Accepted Solutions
best response confirmed by Abhinav Gupta (Copper Contributor)
Solution

Does this work for you:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Intersect(Target, Me.Range("H5:DH1000")) Is Nothing Then Exit Sub
    If Target.Value <> Chr(214) Then
        With Target
            .Value = Chr(214)    'Required
            .Font.Name = "Symbol"    'Required
            .Font.Size = 13
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
        End With
        Cancel = True
    ElseIf Target.Value = Chr(214) Then
        Target.ClearContents
        Cancel = True
    End If
End Sub

View solution in original post