WorksheetFunction.Text() Function not Formatting Correctly in VBA

bemp87

Board Regular
Joined
Dec 10, 2016
Messages
102
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Before this line:
VBA Code:
        Cells(RowNum, 33).Value = TimeString
add this line:
VBA Code:
        Cells(RowNum, 33).NumberFormat = "@"
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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