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
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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

After running this code, I have a problem with another sub that I use to delete rows that contain dates before today. My code used to work but after running your code and truncating the date numbers I get a "type mismatch" error. Here is the code I used.

VBA Code:
    For iRow = LastRow To 2 step -1   
        
        If Cells(iRow, 2).Value <= Now - 1 Then
           Cells(iRow, 2).EntireRow.Delete
        End If
        
    Next IRow
 
Upvote 0
Sounds like you have a non-numeric value somewhere.
When you get that error, if you hover over the "iRow" variable, what does it show for the value of that variable?
And then what is the exact value in column B of that particular row.
 
Upvote 0
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.

I declared it as variant because it is not integer and it is not long.

Here is how the data looks like:


45349.93​
45349.94​
45349.93​
45349.94​
45349.88​
45349.94​
45349.92​
45349.94​
45349.92​
 
Upvote 0
I declared it as variant because it is not integer and it is not long.
You can use "Single" or "Double" to include large numbers and decimals.
Variant will accept ANYTHING, including errors! So I would NOT recommend using that one, unless you plan on adding some error handling!

Please see my last post, and answer the two questions I asked there. That should show you exactly what is causing the code to fail.
 
Upvote 0
You can use "Single" or "Double" to include large numbers and decimals.
Variant will accept ANYTHING, including errors! So I would NOT recommend using that one, unless you plan on adding some error handling!

Please see my last post, and answer the two questions I asked there. That should show you exactly what is causing the code to fail.
You are right, there was one raw that had a non numeric value, now the code works perfectly.

Thank you for your help, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
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