WorksheetFunction.Text() Function not Formatting Correctly in VBA

bemp87

Board Regular
Joined
Dec 10, 2016
Messages
100
I have a Column that stores a cumulative time value as 95:31:00 (for example) that is of the format [h]:mm:ss. I am needing to convert this to a Text / String value which i know can be done using the worksheet function Text(timeval, "[hh]:mm:ss"), but i am curious if the same can be accomplished programmatically using VBA. I have the following code but it keeps inserting the value as a datetime format instead of a string? The Timestring is not returning as an actual string value capturing the values as "[hh]:mm:ss". What am i Doing Wrong?

VBA Code:
Sub DataCleanse()
    Dim RowNum As Long
    Dim CpH As Double
    Dim StatDate As Date
    Dim EomFlag As Integer
    Dim TimeString As String
    
    
    StatDate = "05/22/2020"
    EomFlag = 0
    RowNum = 2
    For RowNum = 2 To 19
        CpH = Round(Cells(RowNum, 13).Value / (Cells(RowNum, 11).Value * 24), 2)
        TimeString = WorksheetFunction.Text(Cells(RowNum, 11), "[h]:mm:ss")
        Cells(RowNum, 15).Value = CpH
        Cells(RowNum, 16).Value = StatDate
        Cells(RowNum, 17).Value = EomFlag
        Cells(RowNum, 33).Value = TimeString
    Next RowNum
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,916
Office Version
  1. 365
Platform
  1. Windows
Before this line:
VBA Code:
        Cells(RowNum, 33).Value = TimeString
add this line:
VBA Code:
        Cells(RowNum, 33).NumberFormat = "@"
 

Watch MrExcel Video

Forum statistics

Threads
1,129,549
Messages
5,636,936
Members
416,953
Latest member
prakashkumar

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
Top