PDA

View Full Version : [SOLVED:] Help with a Macro to insert a column of a fairly complicated Date/Time function



dbell
10-06-2023, 12:45 PM
I am new to this Forum, and it appears I'm NOT doing this correctly!

I created a long post, containing explanation, sample data, and Macro code I am trying to debug.
When I Submit, the post is denied, with the only explanation that new posts my be denied due to excessive URLs (I had none), or "prohibited words", which I sincerely hope I wasn't using (not that I know what those might be!)

So far, this passes Preview, so hopefully acceptable.
I'll try attaching a Word doc where I saved the text!

Thanks for any help!
Dave

June7
10-06-2023, 02:21 PM
I tried ActiveCell.Formula and that worked.

It is not necessary to actually Select anything. I was able to set formula in each cell with this:

Sub AGS_Date()
Dim r As Integer
r = Range("A" & Rows.Count).End(xlUp).Row
Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Value = "Time"
Range("A2:A" & r).Formula = "=DATE(LEFT(B2,4),MID(B2,5,2),MID(B2,7,2)) + TIME(MID(B2,10,2), MID(B2,12,2), MID(B2,14,2))+MID(B2,16,4)/86400"
Range("A2:A" & r).NumberFormat = "0.0000000000"
'Range("A2:A" & r).NumberFormat = "yyyy-mm-dd hh:mm:ss AM/PM"
End Sub

Why replace formula with values? But if you must:

Columns("A").Copy
Columns("A").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Range("A1").Select

dbell
10-06-2023, 02:45 PM
Fantastic! Thank you.
Only fix-up was to use "Dim r As Long"

I forget how much can be done en masse, instead of iteratively.Dave

Aussiebear
10-06-2023, 07:09 PM
Welcome to VBAX Dave. Once you've been posting to the forum a few times, the rules set up by the Admin will ease as to what can be posted.