VBA Code for Date and Time in a Cell and Wrap Text

OilEconomist

Board Regular
Joined
Dec 26, 2016
Messages
194
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance. I will try to post feedback whether it works or not. Also, please feel free to clean up my code as I'm still learning how to write code.

I am trying to set a cell to the date and time in the format of "YYYY-MM-DD", "HH:MM:SS AM/PM"

(1) So first I set the cell to today by using the now function Cell.Formula = Now()
(2) Format the cell to where it will look this for today "2020-05-13, 07:29:10 AM"
I also would like to wrap the text to where "2020-05-13," is on the top and "07:29:10 AM" is underneath it.
I already have the column width set and wrap text set.
Please note, I would like to add a comma in there.

Code:
        'Activate the worksheet
            Worksheets("Sheet1").Activate
           
        'Set the cell to the date and time and format
            'Set Cell to the date and time
                Cells(2, 3).Select
                Cells(2, 3).Formula = "=NOW()"
                Selection.NumberFormat = "yyyy-mm-dd, hh:mm:ss AM/PM"
                Cells(2, 3).Value = Cells(2, 3).Value
           
            'Set the column width and format the cell
                With Selection
                    .ColumnWidth = 11.36
                    .Font.Bold = True
                    .WrapText = True
                End With

Once again thanks!
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Watch MrExcel Video

Forum statistics

Threads
1,114,490
Messages
5,548,356
Members
410,828
Latest member
A9Bosv3
Top