Using TRUNC of FIX function in to extract date part

nhandal

Board Regular
Joined
Apr 18, 2008
Messages
97
Hello,

I have a sheet that includes a list of datetime values in column B, I am using this code to loop through all rows to replace the number in each cell with only the date part of the number using the FIX function, but when I run the code VB returns an error "Object doesn't support this property or method. I can't figure out what is the problem. I appreciate someone's help.

VBA Code:
Sub ConvertDateValues()

    Dim lRow As Long
    Dim lLastRow As Long
    Dim vCellDateNumber As Variant
        
    lLastRow = Worksheets("Sheet1").UsedRange.Rows.Count
    
    For lRow = lLastRow To 2 Step -1
            
        vCellDateNumber = Cells(lRow, 2).Value
        Range(Cells(lRow, 2)).Value = Application.WorksheetFunction.Fix(vCellDateNumber)

    Next lRow

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
There is no FIX function in Excel.
Do you want the dates to be left as dates, rather than being converted to text?
 
Upvote 0
There is no FIX function in Excel.
Do you want the dates to be left as dates, rather than being converted to text?
I initially wanted to use the TRUNC function but it did not work in VBA, I saw posts on some websites that suggest using the FIX function in VBA.
I want to keep the cell values as numbers, because I want to use them as dates but I only want to remove the time part in the number.
 
Upvote 0
"FIX" is a VBA function, not an Excel one.

So instead of referencing it like this:
VBA Code:
... = Application.WorksheetFunction.Fix(vCellDateNumber)
you would just use:
VBA Code:
... = Fix(vCellDateNumber)
 
Upvote 0
Another option is
Excel Formula:
Sub nhandal()
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(@="""","""",int(@))", "@", .Address))
   End With
End Sub
 
Upvote 0
Solution
"FIX" is a VBA function, not an Excel one.

So instead of referencing it like this:
VBA Code:
... = Application.WorksheetFunction.Fix(vCellDateNumber)
you would just use:
VBA Code:
... = Fix(vCellDateNumber)

Thanks, now I get a new error message that says "Type mismatch"
 
Upvote 0
Thanks, now I get a new error message that says "Type mismatch"
It probably means you are trying to apply the FIX function to a non-numeric entry (text or an error, etc).
 
Upvote 0
Another option is
Excel Formula:
Sub nhandal()
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(@="""","""",int(@))", "@", .Address))
   End With
End Sub

Thank you very much, this code works and it is much faster than the loop that used. I appreciate your help.

Just out of curiosity, do you know what is wrong in the code that I used?
 
Upvote 0
How can this be? The values in this column are numbers not text.
Without having access to your data, I really cannot say. It may be picking up something you aren't expecting.

BTW, if all the numbers are numbers, why have you declared the variable as Variant?
VBA Code:
Dim vCellDateNumber As Variant
That seems to suggest that ANYTHING is allowed, and not just numbers.
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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