Error Before Double Click function

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
My code does not work on those weekend columns. Please help to review where goes wrong.
When double click a blank cell, it will be filled with pattern. If it is already filled with pattern, it will change the cell to a blank cell. However it does not work on those weekend columns. I have a function IsWeekend.

TestDropDownList_2.xlsm
ABHIJKLMNO
1MDateAttendance1-Feb2-Feb3-Feb4-Feb5-Feb6-Feb7-Feb8-Feb
2DateSummary12345678
3Larry QT:16 L:0.5 D:2.5 E:0 N:0GGGDND1D3
4Mandy HT:16 L:0.5 D:3.5 E:0 N:0DKDKKK
5John GT:16 L:0 D:1 E:2 N:0D
6Zita VT:16 L:1 D:2 E:0 N:1g
7Peter BT:16 L:2 D:1 E:0 N:0AL
8Nacy LT:16 L:0 D:3 E:0 N:2DGNND
202202
Cells with Data Validation
CellAllowCriteria
A2List=Data!$P$2:$P14
A3List=Data!$P$2:$P$14
A4List=Data!$P$2:$P$13
A5:A6List=Data!$P$2:$P$12
A7List=Data!$P$2:$P$11
A8List=Data!$P$2:$P$10


VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Sh.Name = "Data" Then Exit Sub

On Error Resume Next

Dim lr As Long
Dim Lc As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
Dim rngArea As Range
Set rngArea = Range(Cells(3, 8), Cells(lr, Lc))


Debug.Print rngArea.Address

If Not Intersect(Target, rngArea) Is Nothing Then
   Cancel = True
   If Not IsEmpty(Target.Value) Then Exit Sub
 
      If Target.Interior.Pattern = xlNone = True Then
                   With Target.Interior
                     .Pattern = xlPatternUp
                     .PatternColor = RGB(166, 166, 166)
                   End With
   
      Else
       If Target.Interior.Pattern = xlNone = False Then
             If IsWeekend(Cells(1, Target.Column)) = False Then
                    Target.Interior.Pattern = xlNone
                 
             Else
                If IsWeekend(Cells(1, Target.Column)) = True Then
                      Target.Interior.Pattern = xlNone
                      Target.Interior.Color = RGB(255, 245, 230)
                End If
             End If
       End If
      End If
 
End If
   
End Sub


Public Function IsWeekend(InputDate As Date) As Boolean
 
        Select Case Weekday(InputDate)
        Case vbSaturday, vbSunday
            IsWeekend = True
        Case Else
            IsWeekend = False
    End Select
   
End Function
 

Attachments

  • pattern.png
    pattern.png
    60.6 KB · Views: 8

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Your code for the weekend dates is doing different things than your other code is.
How exactly do you want the weekend dates to react?
 
Upvote 0
Hi Joe4,
Same as other normal cells. If the cell is in the weekend column and is empty, double click trigger it to become patterned; if it is already patterned, then it becomes non patterned but interior color as the whole column.
 
Upvote 0
See if this does what you want:
VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Sh.Name = "Data" Then Exit Sub

On Error Resume Next

Dim lr As Long
Dim Lc As Long
Dim rngArea As Range
Dim wkend As Boolean
Dim pat As Double

lr = Range("A" & Rows.Count).End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column

Set rngArea = Range(Cells(3, 8), Cells(lr, Lc))

Debug.Print rngArea.Address

If Not Intersect(Target, rngArea) Is Nothing Then
    Cancel = True
    If Not IsEmpty(Target.Value) Then Exit Sub
 
'   Get interior pattern number
    pat = Target.Interior.Pattern
    
'   Check to see if weekend
    wkend = IsWeekend(Cells(1, Target.Column))
    
'   Check to see if it has no lined pattern at all
    If (pat = -4142) Or (pat = 1) Then
        Target.Interior.Pattern = xlPatternUp
        Target.Interior.PatternColor = RGB(166, 166, 166)
    Else

'       Check to see if it has lined pattern
        If pat = -4162 Then
'           Check to see if weekend
            If wkend = True Then
                Target.Interior.Pattern = xlNone
                Target.Interior.Color = RGB(255, 245, 230)
            Else
                Target.Interior.Pattern = xlNone
            End If
        End If
    End If
        
End If
   
End Sub
 
Upvote 0
Solution
Hi Joe4, Need help from you.
I enchanced the code to copy from non-empty cell to four adjacent cells. I have already encoded to paste cells except patterned and non empty only but it does not copy to the weekend cells (with color RGB(255,245,230). Please advise where goes wrong ?

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal sh As Object, ByVal Target As Range, Cancel As Boolean)
    If sh.Name = "Data" Then Exit Sub
   
    Application.ScreenUpdating = False
    'Application.EnableEvents = False
   


On Error Resume Next

Dim lrow As Long
Dim Lcol As Long
Dim rngArea As Range
Dim wkend As Boolean
Dim pat As Double

lrow = Range("A" & Rows.Count).End(xlUp).Row
Lcol = Cells(1, Columns.Count).End(xlToLeft).Column

Set rngArea = Range(Cells(3, 8), Cells(lrow, Lcol))

Debug.Print rngArea.Address

If Not Intersect(Target, rngArea) Is Nothing Then
    Cancel = True
    If Not IsEmpty(Target.Value) Then                        
      'Target.Offset(, 1).Resize(, 4).Value = Target.Value
      For col4 = 1 To 4
           Dim adjcell As Range
           Set adjcell = Target.Offset(, col4)
           adjcellpat = Target.Offset(, col4).Interior.Pattern
           adjcellcol = Target.Offset(, col4).Interior.Color
           If adjcellpat <> -4142 Then                                'Cells with any pattern
               
                [COLOR=rgb(226, 80, 65)]ElseIf Target.Offset(, col4).Interior.Color = RGB(255, 245, 230) Then
                       Target.Offset(, col4).Value = Target.Value[/COLOR]
                                       
                ElseIf adjcell.Interior.Color = 16777215 And adjcell.Value = "" And adjcell.Interior.Pattern = xlNone Then
                       Target.Offset(, col4).Value = Target.Value      'Workable
                     
               
           Else
           End If
      Next col4
     
     
     
     
    Else
'ON/OFF FORMATTING CELLS TO PATTERN
'https://www.mrexcel.com/board/threads/error-before-double-click-function.1185307/#post-5774690
'Post 4 Joe4
   
   
'   Get interior pattern number
    pat = Target.Interior.Pattern
   
'   Check to see if weekend
    wkend = IsWeekend(Cells(1, Target.Column))
   
    '   Check to see if it has no lined pattern at all
        'pattern -4142 = xlPatternNone  and pattern 1 = xlPatternSolid
        If (pat = -4142) Or (pat = 1) Then
            Target.Interior.Pattern = xlPatternUp               ' or xlPatternLightVertical
            Target.Interior.PatternColor = RGB(166, 166, 166)
        Else
   
    '       Check to see if it has lined pattern
            'If pat = -4162 Then               'Pattern -4162 = xlPatternUp only
            If Not IsEmpty(pat) Then           'Applicable to any pattern
    '           Check to see if weekend
                If wkend = True Then
                    Target.Interior.Pattern = xlNone
                    Target.Interior.Color = RGB(255, 245, 230)
                Else
                    Target.Interior.Pattern = xlNone
                End If
            End If
        End If
    End If
End If

TestDropDownList_3.xlsm
ABHIJKLMNOPQRST
1MDateAttendance1-Feb2-Feb3-Feb4-Feb5-Feb6-Feb7-Feb8-Feb9-Feb10-Feb11-Feb12-Feb13-Feb
2DateSummary12345678910111213
3Larry QT:16 L:0.5 D:2.5 E:0 N:0GGGDD
4Mandy HT:16 L:0.5 D:3.5 E:0 N:0DKKDK
5John GT:16 L:0 D:1 E:2 N:0D
202202
Cells with Data Validation
CellAllowCriteria
A2List=Data!$P$2:$P14
A3List=Data!$P$2:$P$14
A4List=Data!$P$2:$P$13
A5:A6List=Data!$P$2:$P$12
 

Attachments

  • copyerror4cells.png
    copyerror4cells.png
    54.1 KB · Views: 10
Upvote 0
Hi Joe4, Need help from you.
I enchanced the code to copy from non-empty cell to four adjacent cells. I have already encoded to paste cells except patterned and non empty only but it does not copy to the weekend cells (with color RGB(255,245,230). Please advise where goes wrong ?
That is a new twist on the question, and should probably be posted to a new thread (especially I will not have a chance to look at it today).
 
Upvote 0
Hi Joe4, I created a new thread already.
 
Upvote 0

Forum statistics

Threads
1,223,980
Messages
6,175,763
Members
452,668
Latest member
mrider123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top