VBA Equivalent for Formula

Raymond_India

Board Regular
Joined
Nov 7, 2008
Messages
68
Dear friends,

I have typed the value "200-250" in cell B3.

On cell D3 I type the formula =LEFT(B3,FIND("-",B3-1) and it returns the value 200
On cell E3 I type the formula =RIGHT(B3),LEN(B3)FIND("-",B3) and it returns the value 250
This works perfectly well with formulas.
Please can someone help me with the VBA equivalent for the same thing ?

Regards
Raymond
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Raymond,

Try

the formula for the cell to get the first value:-

=getValueFirstOrLast(B3,"F")

the formula for the cell to get the Last value:-

=getValueFirstOrLast(B3,"L")

Public Function getValueFirstOrLast(varCellvalue As Variant, varType As Variant) As Long

'check if '-' is contained with cell


If InStr(1, varCellvalue, "-") = 0 Then
' '-' not found return 0
getValueFirstOrLast = 0
Exit Function
End If

If varType = "F" Then
getValueFirstOrLast = Trim(Left(varCellvalue, InStr(1, varCellvalue, "-") - 1))
ElseIf varType = "L" Then
getValueFirstOrLast = Trim(Mid(varCellvalue, InStr(1, varCellvalue, "-") + 1))
Else
' First or last not included so return 0
getValueFirstOrLast = 0
End If
End Function


Hope this helps
 
Upvote 0
Im not sure how you want to use it but you could use split:

Code:
x = Split(Range("B3"), "-")(0)
y = Split(Range("B3"), "-")(1)
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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