Calculate a ratio from two numbers in vba

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
=num1/GCD(num1,num2)&":"&num2/GCD(num1,num2)


This formula is a ratio between two numbers. Is there a way to do this in vba? I have 3 labels (Label1 , Label2 , Label3) in a userform. Label1 & Label2 will have the numbers and I would like to output the ratio value to Label3. Can anyone assist

Thank you for kindness


 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello Av8tordude,

Here is the Greatest Common Denominator function in VBA. Add this to either your UserForm or place in a Standard Module.

Code:
' Written:  June 12, 2017
' Author:   Leith Ross
' Summary:  Returns the Greatest Common Divisor of two positive integers.


Function GCD(n1 As Double, n2 As Double) As Double


    Dim div     As Double   ' Divsor
    Dim dvd     As Double   ' Divedend
    Dim r       As Double   ' Remainder
    
        ' // Do not divide the numbers if one of them is zero.
        If n1 = 0 Or n2 = 0 Then
            GCD = n1 + n2
            Exit Function
        End If
        
        ' // Assign the dividend and divisor.
        dvd = n1
        div = n2
        
        ' // Swap the numbers if the dividend is smaller than the divsor.
        If n1 < n2 Then
            div = n1
            dvd = n2
        End If
        
        ' // Apply the Euclid's algorthm.
        Do
            r = dvd Mod div
            If r = 0 Then Exit Do
            dvd = div
            div = r
        Loop
        
        GCD = div
        
End Function
 
Upvote 0
Hi Leith, Thank you so kindly for this, but how do I call this to output the value to my userform label3. Forgive me, but its a little advance for my knowledge.

Thanks
 
Upvote 0
Hello Av8tordude,

Here is the Greatest Common Denominator function in VBA. Add this to either your UserForm or place in a Standard Module.

Code:
' Written:  June 12, 2017
' Author:   Leith Ross
' Summary:  Returns the Greatest Common Divisor of two positive integers.


Function GCD(n1 As Double, n2 As Double) As Double


    Dim div     As Double   ' Divsor
    Dim dvd     As Double   ' Divedend
    Dim r       As Double   ' Remainder
    
        ' // Do not divide the numbers if one of them is zero.
        If n1 = 0 Or n2 = 0 Then
            GCD = n1 + n2
            Exit Function
        End If
        
        ' // Assign the dividend and divisor.
        dvd = n1
        div = n2
        
        ' // Swap the numbers if the dividend is smaller than the divsor.
        If n1 < n2 Then
            div = n1
            dvd = n2
        End If
        
        ' // Apply the Euclid's algorthm.
        Do
            r = dvd Mod div
            If r = 0 Then Exit Do
            dvd = div
            div = r
        Loop
        
        GCD = div
        
End Function

I am guessing that you used Doubles for your variable data type in order to maximize the size of the numbers your function could handle. If so, I think you will be interested in this variation (something I wrote and posted to the old newsgroups when they existed) which will handle numbers up to 28 digits long! If any number being passed in contains more than 15 digits, you must surround that number with quote marks (you can be safe and do that will all passed in numbers) in order to protect the value from being coerced to a Long before the function gets a chance to see it.
Code:
Function GCD(ByVal Num1 As Variant, ByVal Num2 As Variant) As Variant
  Dim Remainder As Variant
  If Num1 = 0 Or Num2 = 0 Then
    GCD = Num1 + Num2
  ElseIf Len(Num1) < 29 And Len(Num2) < 29 Then
    Do
      Remainder = Num2 - Int(CDec(Num2) / Num1) * Num1
      Num2 = Num1
      Num1 = Remainder
    Loop While Remainder
    GCD = Num2
  End If
End Function
 
Upvote 0
Hello Av8tordude,,

Assuming you have num1 and num2 defined in your UserForm then you would assign the ratio like this...

Label3.Caption = num1/GCD(num1,num2)&":"&num2/GCD(num1,num2)
 
Last edited:
Upvote 0
Thank you both. Greatly appreciate your kindness and help
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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