Only fire Macro if cell is empty?

Status
Not open for further replies.

aherzog

New Member
Joined
May 27, 2015
Messages
33
I posted a thread about how to add to an already existing VBA Macro the other day. I basically wanted two columns in my spreadsheet to automatically add the present time with one click of the cell. The macro worked, except for the fact that if I clicked the cell again (even by accident) then it would update the originally entered time to the new, current time. Here was the original macro before the edit:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 6 Then Exit Sub
If Not Intersect(Target, Range("F:H")) Is Nothing Then
With Target
.Value = Time
.NumberFormat = "h:mm AM/PM"
End With
End If


A commenter suggested that I add
If Target <> "" Then Exit Sub
after
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
in order to correct this error. It worked, at first. But then I noticed an error message popping up every time I selected more than one cell at a time. How can I get the same results of keeping the original time locked in even if I click on the cell again, without this error message popping up? Thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,517
Messages
6,119,984
Members
448,935
Latest member
ijat

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