Click cell to run a code

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
[FONT=&quot]Hello,[/FONT]
[FONT=&quot]I have a code that runs with a Command button. Is it possible to modify the code so that it runs when I click a particular cell?[/FONT]
[FONT=&quot]This is my code:[/FONT]
[FONT=&quot]Private Sub CommandButton1_Click()[/FONT]
[FONT=&quot]Dim c As Range, a[/FONT]
[FONT=&quot] Set c = Worksheets("Sheet1").Range("A1")[/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 Sub[/FONT]
[FONT=&quot]Thanks in advance[/FONT]
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
There is a double click event:

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

MsgBox Target.Address

End Sub
 
Upvote 0

Forum statistics

Threads
1,226,113
Messages
6,189,049
Members
453,522
Latest member
Seeker2025

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