Worksheet change offset BUT ignore if offset value exists

john316swan

Board Regular
Joined
Oct 13, 2016
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
I have a worksheet change macro that both updates an offset cell value with a date & a selection of cells with corresponding offset dates, however I am trying to update it so that it only inputs date if the offset cell is blank. Here is the code that works:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Column = 23 And Target.Cells.Count > 1 Then
    Application.EnableEvents = False
        For Each cell In Selection
            If cell.Value = "done" Then
                Target.Offset(0, 1) = Date 'However it just overwrites any previous date that was entered
            End If
        Next cell
    Application.EnableEvents = True
    Exit Sub
End If

'This portion of the code works perfectly, I just can't get the selection to work properly    
If Target.Column = 23 Then
    If Target.Value = "done" And Target.Offset(x, 1) < 1 Then
        Application.EnableEvents = False
        Target.Offset(x, 1) = Date
        Application.EnableEvents = True
    End If
End If
    
End Sub

Here is the code that doesn't work (run time error '13' type mismatch):

Private Sub Worksheet_Change(ByVal Target As Range)


Code:
If Target.Column = 23 And Target.Cells.Count > 1 Then
    Application.EnableEvents = False
        For Each cell In Selection
            If cell.Value = "done" Then
                If Target.Offset(0, 1) < 1 Then 'This is line of error
                    Target.Offset(0, 1) = Date
                End If
            End If
        Next cell
    Application.EnableEvents = True
    Exit Sub
End If

'This portion of the code works perfectly, I just can't get the selection to work properly 
If Target.Column = 23 Then
    If Target.Value = "done" And Target.Offset(x, 1) < 1 Then
        Application.EnableEvents = False
        Target.Offset(x, 1) = Date
        Application.EnableEvents = True
    End If
End If
    
End Sub

Thanks in advance for your help, hopefully this is low hanging fruit.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Since you are looping through each cell in the Target, you should be using cell, not Target.
I would recommend changing this whole block:
Code:
        For Each cell In Selection
            If cell.Value = "done" Then
                If Target.Offset(0, 1) < 1 Then 'This is line of error
                    Target.Offset(0, 1) = Date
                End If
            End If
        Next cell
to this:
Code:
        For Each cell In [COLOR=#ff0000]Target[/COLOR]
            If cell.Value = "done" Then
                If [COLOR=#ff0000]cell.[/COLOR]Offset(0, 1) < 1 Then 
                    [COLOR=#ff0000]cell[/COLOR].Offset(0, 1) = Date
                End If
            End If
        Next cell
 
Last edited:
Upvote 0
Solution
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 23 Then
        Application.EnableEvents = False
            For Each [COLOR=#0000ff]cell[/COLOR] In Target
                If [COLOR=#0000ff]cell[/COLOR].Value = "done" Then
                    If [COLOR=#0000ff]cell[/COLOR].Offset(0, 1).Value = "" Then
                        [COLOR=#0000ff]cell[/COLOR].Offset(0, 1) = Date
                    End If
                End If
            Next cell
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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