VBA Code for Entering Today Function

jevi

Active Member
Joined
Apr 13, 2010
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am trying this VBA as I need in sheet1 at column starting from i2, if i2 is empty to put the today date, if cell i2 has a date it should leave the old date. I did a formula =IF(I2="";TODAY();I2),but I was still thinking why the code is not working.
Maybe I should put .cell (i2) and not range.

HTML:
Private Sub Workbook_Open()With Sheets("sheet1").Range("I2:I200")If .Value = "" Then .Value = DateEnd WithEnd Sub

thank you
 
Not sure I am understanding your question exactly but try the code below on a copy of your data and change the date format to suit.

Code:
Private Sub Workbook_Open()
    Dim myCell As Range
    Application.ScreenUpdating = False
    For Each myCell In Sheets("sheet1").Range("I2:I" & Sheets("sheet1").Range("I" & Rows.Count).End(xlUp).Row)
        With myCell
            If .Value = "" Then .Value = Date
            .Value = Right(.Offset(, 4).Value, 8) & " " & Format(.Value, "mm/dd/yyyy")
        End With

    Next
    Application.ScreenUpdating = True
End Sub
I am still not sure about the 8 digits part, but if you are correct in the assumptions embodied in your code, then the result can be achieved without using a loop. This code will do the same as your code does...
Code:
Private Sub Workbook_Open()
  With Range("I2", Cells(Rows.Count, "I").End(xlUp))
    .Cells = Evaluate("IF(" & .Address & "=""""," & .Offset(, 4).Address & "&TEXT(TODAY(),"" mm/dd/yyyy"")," & .Address & ")")
  End With
End Sub
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
then the result can be achieved without using a loop. This code will do the same as your code does...

I thought there would be a way to do it with Evaluate but I haven't got used to the syntax yet (and lack of time to practice these days) so I am pleased you posted the reply so I have something else to look back at when I get the chance.
 
Upvote 0
Hi Pluff, Thank you for your vba code. It did what I need even though I had to put (;) and not (,) as here we use ;. But then it showed the formula in the cell as:
=RIGHT(B2; 8)

So I had to clear formats; put in F2 and then press enter. Still Thank you so much :).
 
Upvote 0
Thank you Mark, sorry if my eplanation was not good.

Still your second code worked as a treat for the date:).

But I wanted for the same sheet1 which in column B2:B200, I have text with 15 digits, I needed to put in column M2:M200 with 8 digits. So I thought it should be a way to combine both the VBA code. Thank you.
 
Upvote 0
Hi Rick, sorry if my explanation was not good.

I wanted to combine the VBA of Mark (the second one at the begining for the date) with another work I have to do for the same sheet.

So I have in columns B2:B200, text with 15 digits, I needed to put them in column M2:M200 (or even at the same column B2B:B200) with 8 digits.

Please, let me know if I am not clear. Thank you so much for your kind help to all.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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