VBA to find HCF/GCD

Alexis12345

New Member
Joined
Oct 12, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I have the following codes for VBA excel to compute GCD of 2 numbers, how can I continue from here to compute GCD of 3 numbers?

Sub GCD()

Dim small As Integer, large As Integer
Dim temp As Integer, remainder As Integer, k As Integer

' input the two positive integers
small = InputBox("Enter the first number: ")
large = InputBox("Enter the second number: ")

' make sure that L is the larger one and S is the smaller one:
If small > large Then
temp = large
large = small
small = temp
End If

k = 0 ' counter to keep track of the number of iterations
Do
remainder = large Mod small
large = small
small = remainder
k = k + 1 ' increase counter by 1
Loop While remainder <> 0

MsgBox "After " & k & " iterations, we found the GCD: " & large

End Sub
 

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.
Hi & welcome to MrExcel.
Why not just use the inbuilt Excel function?
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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