How to concatenate formula and string characters using function code

vbvba

New Member
Joined
May 25, 2012
Messages
34
Hi,

I have written a function code to concatenate strings and formula, which is failing after so many attempts.


Function CAT_SUM(a As Integer, b As Integer)
CAT_SUM = a - b
If CAT_SUM.Value < 0 Then
ActiveCell = "the percentage has decreased by " & CAT_SUM & ""
Else
ActiveCell = "the percentage has increased by " & CAT_SUM & ""
End If
End Function




The aim behind this code is that if I subtract A2:A65536 cell value and B2:B65536 cell value, based on the result (positive or negative) it should display in C2:C65536 value as "The value has increased by " the value" (if it is positive") or "The value has decreased by " the value" (if it is negative"), the function formula should apply to all the cells starting from C2 (depends if the value is there in A and B cells).

Please assist me in the code using function :eek:
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try:

Function CAT_SUM(a As Range, b As Range) As String
CAT_SUM = a - b
If a - b < 0 Then
CAT_SUM = "the percentage has decreased by " & CAT_SUM & ""
Else
CAT_SUM = "the percentage has increased by " & CAT_SUM & ""
End If
End Function

Hope it works!!
 
Upvote 0
Put something like this in C1 to evaluate A1-B1

=IF(AND(A1<>"",B1<>""),"the percentage has " & IF(A1>B1,"increased by ","decreased by ") & ABS(A1-B1), "")
 
Upvote 0
It's not completely clear what you're trying to do.. how's this?
Code:
Function CAT_SUM(a As Range, b As Range)
Dim temp As Single
temp = a.Value - b.Value
If temp < 0 Then
    CAT_SUM = "The % has decreased by " & Math.Abs(temp) & ""
Else
    CAT_SUM = "The % has increased by " & temp & ""
End If
End Function
It's worth pointing out that it's easier to achieve this with worksheet functions but if the purpose of this exercises is to teach yourself VBA then this code should do the trick.
 
Upvote 0
Hi,

Thanks for all your assistance on providing me perfect solution, specially stoolpidgeon code has helped me to learn different way of writing code using function.
However, now I would like to know if I want to change a bit in the query which I have requested.
i.e. the result of A-B will be available in C column, based on the value in C column positive or negative the string character's (i.e. "The % has decreased by " & Math.Abs(temp) ), should appear in D column, please assist me in tweaking the code to acheive this.
 
Upvote 0
Please ignore my previous question, I have solved the issue succesfully.
Thanks for all your help which will help me to learn and improvise in VBA.
 
Upvote 0

Forum statistics

Threads
1,216,819
Messages
6,132,886
Members
449,767
Latest member
Controltower

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