New Function: Creating a ratio using a range.

C_Leam

New Member
Joined
Sep 28, 2022
Messages
6
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. Web
I have successfully created a ratio using the following code below:

Function RATIO(num1, num2) As Variant

With Application
RATIO = (num1 / WorksheetFunction.Gcd(num1, num2)) & ":" & (num2 / WorksheetFunction.Gcd(num1, num2))
End With

End Function

Result returns as such:
1664341821884.png


What I would like to do now is take it further and be able to take it further and allow it to do a ratio within a range. End result should look like this:
1664342222627.png


I know it means starting the code off with Function RATIO(range) As Variant. Then when I enter the formula I'll need to have one line for the first cell in the range and then a loop for all the other cells so they start with ":" followed by the number.

Any help please?
 

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
Welcome to the MrExcel board!

Why not just use worksheet functions directly?

22 09 28.xlsm
ABCDEFG
14010:34020:6:2:1
21212
34
42
Ratios
Cell Formulas
RangeFormula
C1C1=TEXTJOIN(":",,A1:A2/GCD(A1:A2))
G1G1=TEXTJOIN(":",,E1:E4/GCD(E1:E4))
 
Upvote 0
Welcome to the MrExcel board!

Why not just use worksheet functions directly?

22 09 28.xlsm
ABCDEFG
14010:34020:6:2:1
21212
34
42
Ratios
Cell Formulas
RangeFormula
C1C1=TEXTJOIN(":",,A1:A2/GCD(A1:A2))
G1G1=TEXTJOIN(":",,E1:E4/GCD(E1:E4))
Not bad. First time I've used TEXTJOIN.
I like it, but I want to put in my own functions to save typing and time. I've done one for calculating an average without the highest and lowest values which I call an Olympic Average:
Function OLYAVG(range) As Variant
'Caculate average without min and max
With Application
OLYAVG = (.Sum(range) - .Max(range) - .Min(range)) / (.Count(range) - 2)
End With
End Function

I'll see if I can put TEXTJOIN into a VBA coding.

Cheers.
 
Upvote 0
Then what about

VBA Code:
Function RATIOS(r As Range) As String
  RATIOS = Evaluate(Replace("textjoin("":"",,#/GCD(#))", "#", r.Address))
End Function

C_Leam.xlsm
EFG
14020:6:2:1
212
34
42
Ratios
Cell Formulas
RangeFormula
G1G1=RATIOS(E1:E4)
 
Upvote 0
Solution
Then what about

VBA Code:
Function RATIOS(r As Range) As String
  RATIOS = Evaluate(Replace("textjoin("":"",,#/GCD(#))", "#", r.Address))
End Function

C_Leam.xlsm
EFG
14020:6:2:1
212
34
42
Ratios
Cell Formulas
RangeFormula
G1G1=RATIOS(E1:E4)
Perfect.

Thanks Peter
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,292
Members
449,149
Latest member
mwdbActuary

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