VBA Function

ziad alsayed

Well-known Member
Joined
Jul 17, 2010
Messages
665
dear all

i am using the below function and it is working, but if i use in a cell that is formatted is percentage (60%, 80%....) it will not work.

appreciate any assistance


Code:
Function GetRemarks(rng As Range) As String
Dim Appraisal As Byte
Appraisal = rng.Value
Select Case Appraisal
    Case 60 To 79
    GetRemarks = "Meet Expectation"
    Case Is > 80
    GetRemarks = "Above Expectation"
    Case Is < 60
    GetRemarks = "Below Expectation"
End Select
End Function
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:

Code:
Function GetRemarks(v As Variant) As String
    Select Case v
        Case Is >= 0.8
            GetRemarks = "Above Expectation"
        Case Is >= 0.6
            GetRemarks = "Meet Expectation"
        Case Else
            GetRemarks = "Below Expectation"
    End Select
End Function
 
Upvote 0
Or ...
Code:
Function GetRemarks(v As Variant) As String
    GetRemarks = WorksheetFunction.Lookup(v, Array(0, 0.6, 0.8), _
                                          Array("Below", "Meet", "Above")) & _
                                          " Expectation"
End Function
 
Upvote 0
thanks Shg it is working, but if i am using as per below it is not, can you please explain why it is not working, it is only giving meet expectation

Code:
Function GetRemarks(rng As Range) As String
Dim Appraisal As Byte
Appraisal = rng.Value
Select Case Appraisal
    Case 0.6 To 0.79
    GetRemarks = "Meet Expectation"
    Case Is > 0.8
    GetRemarks = "Above Expectation"
    Case Is < 0.6
    GetRemarks = "Below Expectation"
End Select
End Function
 
Upvote 0
Type Byte stores unsigned integers 0 to 255, not decimal values.
 
Upvote 0
thanks again :):), i like the second code, i hope one day i will able to write such code

i really appreciate your assistance.
 
Upvote 0
if i use in a cell that is formatted is percentage (60%, 80%....) it will not work.
20% is really 0.2 (format the cell as General and you'll see)
Compounded by using the Byte data type which will change values below 1 to 0. Use Double or Single instead?
Also what if your value is 0.8 (80%)? It's not catered for. Try
Code:
Function GetRemarks(rng As Range) As String
Dim Appraisal As Double
Appraisal = rng.Value
Select Case Appraisal
  Case Is < 0.6
    GetRemarks = "Below Expectation"
  Case Is < 0.8
    GetRemarks = "Meet Expectation"
  Case Else
    GetRemarks = "Above Expectation"
End Select
End Function
 
Upvote 0
thanks Shg, after your remarks i check excel help and dim the variable as variant

PHP:
At this time the Decimal data type can only be used within a Variant, that is, you cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the CDec function
.
 
Upvote 0
dear P45cal

Long time, hope you are fine and doing well.

i tried it as double or single and it works also.

thanks for drawing my attention to the 80%, you are right , i changed the condition

thanks
 
Upvote 0
You don't need a variable at all ...
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

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