Runtime Error: 1004 - Execute Macro when a condition is met in two columns

Tombies

New Member
Joined
Jun 15, 2014
Messages
2
First off, sorry to ask about runtime error 1004, but I am not that familiar with VBA and I cannot find a solution anywhere that I can make sense of.


The problem I get is: Run-Time error '1004' : Application-defined or object-defined error

Here is my code:

Code:
Sub DateInsertion()
'
' DateInsertion Macro
'
' Keyboard Shortcut: Ctrl+m


Dim QuoteSubmission As String, DateSubmitted As Range


    With Sheets("T3169")
        QuoteSubmission = .Range("H2:H" & Rows.Count).End(x1Up).Row
        For Each DateSubmitted In .Range("I2:I" & QuoteSubmission)
            If (DateSubmitted.Value = "") And (DateSubmitted.Offset(0, -1) = "Yes") Then
            DateSubmitted.Value = Date
        
            End If
        Next DateSubmitted
    End With
 


End Sub


The excel sheet itself consists of column "H" with Yes or No. Column "I" is a short date. When a cell in Row Nth, col. 'H' = "Yes", I want the adjacent cell in col. 'I' to enter the date.

The excel sheet is a dynamic sheet and it updates from a database. Upon refresh, if a cell in column 'H' changes to "Yes", the VBA code does not detect the change, hence it will not enter the date. That is why I am using a macro with a shortcut to do it rather then programming a "Sub Worksheet_Change".

Any help is appreciated!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe this, which will update when the worksheet recalcs
Code:
Sub Workbook_worksheetCalculate()
'
' DateInsertion Macro
'
' Keyboard Shortcut: Ctrl+m


Dim QuoteSubmission As String, DateSubmitted As Range, lr As Long
lr = Sheets("T3169").Cells(Rows.Count, "H").End(xlUp).Row
    With Sheets("T3169")
      For r = 2 To lr
            If Range("I" & r).Value = "" And Range("H" & r).Value = "Yes" Then
            Range("I" & r).Value = Date
            End If
        Next r
    End With
End Sub
 
Upvote 0
Maybe this, which will update when the worksheet recalcs
Code:
Sub Workbook_worksheetCalculate()
'
' DateInsertion Macro
'
' Keyboard Shortcut: Ctrl+m


Dim QuoteSubmission As String, DateSubmitted As Range, lr As Long
lr = Sheets("T3169").Cells(Rows.Count, "H").End(xlUp).Row
    With Sheets("T3169")
      For r = 2 To lr
            If Range("I" & r).Value = "" And Range("H" & r).Value = "Yes" Then
            Range("I" & r).Value = Date
            End If
        Next r
    End With
End Sub

It worked! Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,410
Messages
6,124,755
Members
449,187
Latest member
hermansoa

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