Modify code

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
[FONT=&quot]I have this code (below) that works for worksheet "CHIT". I want to modify this code so that it works for another worksheet "CHIT2".[/FONT]
[FONT=&quot]So if I double click on worksheet "CHIT", it also works on worksheet "CHIT2".[/FONT]
[FONT=&quot]Any help is appreciated[/FONT]
[FONT=&quot]CODE:[/FONT]
[FONT=&quot]Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)[/FONT]
[FONT=&quot]
[/FONT]

[FONT=&quot]Dim c As Range, a[/FONT]
[FONT=&quot] If Target.Address = "$K$8" Then[/FONT]
[FONT=&quot] ' Don't edit the cell[/FONT]
[FONT=&quot] Cancel = True[/FONT]
[FONT=&quot] ' Increment the invoice nunber[/FONT]
[FONT=&quot] Set c = Worksheets("CHIT").Range("J8")[/FONT]
[FONT=&quot] c.NumberFormat = "@"[/FONT]
[FONT=&quot] If InStr(c, "/") = 0 Then[/FONT]
[FONT=&quot] c = "000001/" & Format(Date, "yy")[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] a = Split(c, "/")[/FONT]
[FONT=&quot] a(0) = Format(a(0) + 1, "00000#")[/FONT]
[FONT=&quot] If Format(a(1), "00") <> Format(Date, "yy") Then a(1) = Format(Date, "yy")[/FONT]
[FONT=&quot] c = Join(a, "/")[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot]
[/FONT]

[FONT=&quot]End Sub[/FONT]
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Remove the event from your "CHET" sheet and put the following event on ThisWorkbook

The event will work for the 2 sheets

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim c As Range, a
    Select Case LCase(Sh.Name)
        Case LCase("CHIT"), LCase("CHIT2")
            If Target.Address = "$K$8" Then
                ' Don't edit the cell
                Cancel = True
                ' Increment the invoice nunber
                Set c = Sh.Range("J8")
                c.NumberFormat = "@"
                If InStr(c, "/") = 0 Then
                    c = "000001/" & Format(Date, "yy")
                Else
                    a = Split(c, "/")
                    a(0) = Format(a(0) + 1, "00000#")
                    If Format(a(1), "00") <> Format(Date, "yy") Then a(1) = Format(Date, "yy")
                    c = Join(a, "/")
                End If
            End If
    End Select
End Sub
 
Upvote 0
Hi DanteAmor
Sorry if I don't give a good explanation. It works but I need that if I double click on any worksheet , it works for both. So the number will be the same for both worksheets.
If I double click on worksheet "CHIT", it counts 2 and worksheet "CHIT2" counts 1.

Thanks
 
Last edited:
Upvote 0
Try the following at the same Thisworkbook event

Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim c As Range, a
    If Target.Address = "$K$8" Then
        ' Don't edit the cell
        Cancel = True
        ' Increment the invoice nunber
        
        Set c = Sheets("CHIT").Range("J8")
        c.NumberFormat = "@"
        If InStr(c, "/") = 0 Then
            c = "000001/" & Format(Date, "yy")
        Else
            a = Split(c, "/")
            a(0) = Format(a(0) + 1, "00000#")
            If Format(a(1), "00") <> Format(Date, "yy") Then a(1) = Format(Date, "yy")
            c = Join(a, "/")
        End If
        '
        Set c = Sheets("CHIT2").Range("J8")
        c.NumberFormat = "@"
        If InStr(c, "/") = 0 Then
            c = "000001/" & Format(Date, "yy")
        Else
            a = Split(c, "/")
            a(0) = Format(a(0) + 1, "00000#")
            If Format(a(1), "00") <> Format(Date, "yy") Then a(1) = Format(Date, "yy")
            c = Join(a, "/")
        End If
    
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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