Matt Gaydon
Matt Gaydon

Reputation: 117

GCD Function in VBA Returning Too Many Digits

I want to return a ratio in my Excel sheet using VBA. I have two columns of data: repaired defects and unrepaired defects. The ratio would be the number of defects a crew repaired to the number of defects they could not repair. I am using this function to find the GCD:

Function GCD(numerator, denominator)
    If denominator = 0 Then
        GCD = numerator
    Else 
        GCD = GCD(denominator, numerator Mod denominator)
    End If
End Function

Then I calculate it and print it in the Results page of my spreadsheet with:

Sheets("Results").Range("M" & iResults)/Value = RatioNum1 & ":" & RatioNum2

Where RatioNum1 and RatioNum2 are simply variables where I have stored Data1 / GCD and Data2 / GCD. My problem is that in Excel, say the ratio is 1 to 1, it prints the second number with an extra digit every time, in this example 1:01. Or if it's supposed to be 3:1, it prints 3:01 and so on.

Is this simply the way the function returns values or am I inadvertently adding the extra digit somewhere with my code.

Upvotes: 0

Views: 425

Answers (1)

Miqi180
Miqi180

Reputation: 1691

To remove the zero in front of the second ratio number, simply use the Int function like this:

Sheets("Results").Range("M" & iResults)/Value = RatioNum1 & ":" & Int(RatioNum2)

Upvotes: 1

Related Questions