I've cleaned up and refactored to eliminate the redundant code, and made explicit all implicit ActiveSheet references:
Private Sub Workbook_Open() Const startCol As Long = 1 Const colCountToSet As Long = 5 Const skipColCount As Long = 3 Dim endRow As Long endRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row If IsDate(Cells(endRow, startCol)) Then If Int(CDate(ActiveSheet.Cells(endRow, startCol))) <> Date Then endRow = endRow + 1 Dim curCol As Long curCol = startCol Dim counter As Integer For counter = startCol To colCountToSet ActiveSheet.Cells(endRow, curCol) = Date curCol = curCol + skipColCount Next End If End If End Sub
Here's what was done and why:
Const startCol As Long = 1 Const colCountToSet As Long = 5 Const skipColCount As Long = 3
If you ever need to add or remove a set of columns, adjust colCountToSet and your code continues to work.
If you add another column to each set or add space between data sets, adjust skipColCount and your code continues to work.
If you insert a new col A, adjust startCol
Dim todaysDate As String todaysDate = Format(Now(), "mm/dd/yyyy")
There's no sense in calling the Format() function multiple times, you're only interested in the date, and if someone happens to open the workbook moments before midnight, you could possibly get different dates on the same row.
If IsDate(ActiveSheet.Cells(endRow, startCol)) Then
I've fixed up this bit thanks to a suggestion from @Comintern. First ensure your last row contains a date. If, for some reason, someone has entered a non-date value at the bottom, this will skip overwriting it.
If Int(CDate(ActiveSheet.Cells(endRow, startCol))) <> Date Then
The Date function returns a date (as an integer) without the time, so compare that to what's in the last row.
If Format(ActiveSheet.Cells(endRow, startCol), "mm/dd/yyyy") <> todaysDate Then
If the last row is empty, it won't match. If it's yesterday's date, it won't match. Either case falls into the If statement. If it's today's date, it will match and it will skip the If statement. You have to format the date in the column to exactly match the format you're using, since the display formatting of the cell could return a different looking string than what you're testing for.
endRow = endRow + 1
Get rid of the .Offset(). Especially since you're not using a Range object.
For counter = startCol To colCountToSet ActiveSheet.Cells(endRow, curCol) = Date curCol = curCol + skipColCount Next
A nice, simple little loop that will set each column's date without having to adjust anything other than a CONST or two at the very top of the code should your formatting ever change. It sets the date to the system returned Date integer.
Workbook_Open()code, then hit F8. That will start debugging your code, continue to hit F8 one line at a time and watch what your code does as it executes each line of code. Is it doing what you want? B) Instead of using.Offset(), setendRow = endRow + 1, thenCells(endRow,DxCol) = ...to eliminate the somewhat annoying (in my opinion) use of.Offset()