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

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
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!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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