Runtime error '1004' no cells were found for Worksheet_Change

brocazaria

New Member
Joined
Mar 12, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I've attached my code below. The code works as I intended, but after running it displays the error "Runtime error '1004' no cells were found".

Basically I want the code to change cell G3 to display today's date and return the value, if the formula in cell F3 changes. Anyone have any suggestions as to how to get rid of the error, or show me how to make the code run properly?


Private Sub Worksheet_Change(ByVal Target As Range)

' When cell G3 (a formula referencing other cells) is changed, run the code
' Changes cell G3 to post the current date and time and return the value
If Target.Dependents.Address = Range("F3").Address Then
'
' NOW Macro
' Makes the cell display today's date
'
Range("G3").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("G3").Select
Selection.NumberFormat = "d-mmm h:mm"
'
' PasteVal Macro
' Copies the current cell and pastes the value
'
Range("G3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi.

Save this in to the macro enabled workbook and I think it'll work for you. I think the OP had incorrect cell references, so I've adjusted here. Let me know if I'm missing something.



VBA Code:
Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Target = Sh.Range("F3") Then

Range("G3").Value = Format(Now(), "d-mmm h:mm")

End If
End Sub
 
Upvote 0
Hi & welcome to MrExcel
If the formula looks at cell on the same sheet, try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim KyCell As Range

   Set KyCell = Range("F3")
   On Error Resume Next
   Set KyCell = Union(KyCell, KyCell.Precedents)
   On Error GoTo 0
   If Not Intersect(Target, KyCell) Is Nothing Then
      With Range("G3")
         .Value = Now
         .NumberFormat = "d-mmm h:mm"
      End With
   End If
End Sub
 
Upvote 0
Hi.

Save this in to the macro enabled workbook and I think it'll work for you. I think the OP had incorrect cell references, so I've adjusted here. Let me know if I'm missing something.



VBA Code:
Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Target = Sh.Range("F3") Then

Range("G3").Value = Format(Now(), "d-mmm h:mm")

End If
End Sub


Thank you for your reply. The code works well, the only thing is i still need it to return the value of the cell, or else every time i open the excel sheet it will update the date to the current date and time. Do you have any suggestions for this?
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,081
Members
449,205
Latest member
Healthydogs

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