Excel Formula to VBA

dhosi439

Board Regular
Joined
May 13, 2009
Messages
62
Can someone please assist with writing this formula in VBA:

=IF(RIGHT(TEXT(C2,"mm/dd/yy hh:mm:ss"),2)>"30",LEFT(C2,14)+0.000694,C2)

This is what I have so far, but I am receiving a type mismatch error. Please be aware this is a test Sub that I am using via a command button on a sheet. Ultimately this will be transfered into Outlook to pull date/time appropriately.


Code:
Private Sub CommandButton1_Click()
'=IF(RIGHT(TEXT(C2,"mm/dd/yy hh:mm:ss"),2)>"30",LEFT(C2,14)+0.000694,C2)
If Right(Format(ActiveCell.Offset(0, -4).Value, "mm/dd/yy hh:mm:ss"), 2) > "30" Then
ActiveCell.Value = (Left(ActiveCell.Offset(0, -4).Value, 14) + 0.000694)
Else
ActiveCell.Value = Left(ActiveCell.Offset(0, -4).Value, 14)
End If
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Rich (BB code):
Private Sub CommandButton1_Click()
'=IF(RIGHT(TEXT(C2,"mm/dd/yy hh:mm:ss"),2)>"30",LEFT(C2,14)+0.000694,C2)
If Right(Format(ActiveCell.Offset(0, -4).Value, "mm/dd/yy hh:mm:ss"), 2) +0 > 30 Then
ActiveCell.Value = (Left(ActiveCell.Offset(0, -4).Value, 14) + 0.000694)
Else
ActiveCell.Value = Left(ActiveCell.Offset(0, -4).Value, 14)
End If
End Sub

lenze
 
Upvote 0
Okay, the change you presented didn't resolve my issue. But it did prompt me to realize that the error only occurs for this line:

Code:
ActiveCell.Value = (Left(ActiveCell.Offset(0, -4).Value, 14) + 0.000694)

Which is the code that runs when values are over 30.
 
Upvote 0
mikerickson,

I'm actually not reading the seconds value. I am reading the milliseconds.

This is the new code:

Code:
Private Sub CommandButton1_Click()
If Right(ActiveCell.Offset(0, -4).Value, 2) > 30 Then
ActiveCell.Value = (Left(ActiveCell.Offset(0, -4).Value, 14) + 0.000694)
Else
ActiveCell.Value = Left(ActiveCell.Offset(0, -4).Value, 14)
End If
End Sub
 
Upvote 0
What would the 14 characters of the cell be? Might

Code:
ActiveCell.Value = Int(ActiveCell.Offset(0, -4)) + DateValue("0:01:00")
 
Upvote 0
The 14 characters are the date and time: "11/18/10 08:30"

I tried the new code, still received a type mismatch error.
 
Upvote 0
This code resolved my issue.

Code:
Sub Test()
    Dim strTime As String
    
    strTime = Format(ActiveCell.Offset(0, -4).Value, "mm/dd/yy hh:mm:ss")
    If Right(strTime, 2) > "30" Then
        ActiveCell.Value = DateValue(Left(strTime, 14)) + TimeValue(Left(strTime, 14)) + 0.000694
    Else
        ActiveCell.Value = DateValue(Left(strTime, 14)) + TimeValue(Left(strTime, 14))
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,157
Members
449,208
Latest member
emmac

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