Inserting formulas changing date/time to number format?

klutch

New Member
Joined
Jun 7, 2018
Messages
38
Code:
Sub insertformulas()

Dim wb As Workbook: Set wb = ActiveWorkbook
With ActiveWorkbook
 Columns("G:G").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("g2").Formula = "=TEXT(E2,mm/dd/yy)&TEXT(F2,hh:mm:ss)"
Range("h2").Formula = "=TEXT(G2-G3,hh:mm:ss)"
Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("H2:H60")
    Range("H2:H60").Select
    Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G60")
    Range("G2:G60").Select
End With

End Sub
Hello, I wrote this macro to do 3 things.
1. insert 2 columns from column G
2. Insert a formula in both new columns (G&H)
3. Copy the formula down

What happens when I run it is columns e&f go from being dates and time format to number format
Ex. 6/27/2018 to 43278
Ex. 8:03:15 to .33559

Then cells G2 and H2 come up as #NAME (due to the change of columns e&f and the formula does not recognize the number format)
Columns G&H do not copy down.

What is causing the date/time format to change in columns e&f and how do I fix it?

Thanks in advance
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Shouldn't the format be between double quotes ?
=TEXT(E2,"mm/dd/yy")&TEXT(F2,"hh:mm:ss")
(same for the next)

(I might be wrong knowing nothing of VBA)
 
Upvote 0
yes but that produces a compile error "Expected: end of statement"

and when I run it, a syntax error is produced
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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