Entering Today's Date based on values in two different cells (VBA)

JayB0730

Board Regular
Joined
Oct 22, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello,

I want to create code where IF cells in Column A are populated & Column I are not populated insert today's date into Column I. I do not want a set range (i.e. A1:A100) as my spreadsheet will continue to increase over time.

Thank you in advance!
Jay
 

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.
Are there any gaps in the middle of column A?
For example, could you have A1 populated, A2 not populated, A3 populated?
Or will all the populated cells in column A be together with no gaps in between?
 
Upvote 0
If there are no gaps in your column A data, try this:
Code:
Sub MyDateInsert()

    Dim lRow As Long
    Dim rng As Range
    
'   Find last row with data in column A
    lRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Set column I range
    Set rng = Range("I1:I" & lRow)
    
'   Find blank cells in column I
    rng.SpecialCells(xlCellTypeBlanks).Value = Date

End Sub
 
Upvote 0
Hi Joe,

This is great, thank you! After I run the Macro & attempt a second time it brings up an error message...is there a way to make it so that a message pops up to alert the user that there are no dates that need to be entered (or whatever message I want to appear)?

Thanks,
Jay
 
Upvote 0
Sure, like this:
Code:
Sub MyDateInsert()

    Dim lRow As Long
    Dim rng As Range
    
'   Find last row with data in column A
    lRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Set column I range
    Set rng = Range("I1:I" & lRow)
    
'   Find blank cells in column I
    On Error GoTo err_chk
    rng.SpecialCells(xlCellTypeBlanks).Value = Date
    On Error GoTo 0
    
    Exit Sub
    

err_chk:
    If Err.Number = 1004 Then
        MsgBox "No dates need to be entered!", vbOKOnly
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
    
End Sub
 
Upvote 0
Joe,

Worked like a charm. Thank you so much! As I am a n00b to VBA & trying to learn as I go, the comments that you included really helped. Thanks again.

- Jay
 
Upvote 0
You are welcome!
Glad I was able to help.:)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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