Various location for Dim...

alee001

Board Regular
Joined
Sep 9, 2014
Messages
154
Office Version
  1. 2010
Platform
  1. Windows
I write 4 code as follow to solve LCM/GCL but I don't know reason why the answer is wrong with various location on Dim r as Integer?
Code:
[FONT=Courier New]Dim m As Integer
 Dim n As Integer
 Dim r As Integer   'location 1
 Sub GCD(m, n)
 r = m Mod n
 If r = 0 Then
 MsgBox n & " is GCD"  'Wrong ans.
Else
 Call GCD(n, r)
 End If
 End Sub

 Private Sub CommandButton1_Click()
 m = InputBox("Input an interger m ")
 n = InputBox("Input an interger n")
 Call GCD(m, n)
 End Sub[/FONT]

Code:
[FONT=Courier New][FONT=Courier New]Dim m As Integer
 Dim n As Integer
 Sub GCD(m, n)
 Dim r As Integer   [FONT=Courier New]'location 2[/FONT]
 r = m Mod n
 If r = 0 Then
 MsgBox n & " is GCD" 'Correct ans.
Else
 Call GCD(n, r)
 End If
 End Sub[/FONT]

Private Sub CommandButton1_Click()
 m = InputBox("Input an interger m ")
 n = InputBox("Input an interger n")
 Call GCD(m, n)
 End Sub[/FONT]

Code:
[FONT=Courier New]Dim m As Integer
 Dim n As Integer
 Sub GCD(m, n)
 r = m Mod n
 If r = 0 Then
 MsgBox n & " is GCD"   'Correct ans.
 Else
 Call GCD(n, r)
 End If
 End Sub

 Private Sub CommandButton1_Click()
 Dim r As Integer   [FONT=Courier New]'location 3[/FONT]
 m = InputBox("Input an interger m ")
 n = InputBox("Input an interger n")
 Call GCD(m, n)
 End Sub[/FONT]

Code:
[FONT=Courier New]Dim m As Integer
 Dim n As Integer
 Dim r As Integer   'location 4
 Sub GCD(m, n)
 Do
 r = m Mod n
 If r <> 0 Then
 m = n: n = r
 End If
 Loop While r <> 0
 MsgBox n & " is GCD"   'Correct ans.
 End Sub

 Private Sub CommandButton1_Click()
 m = InputBox("Input an interger m ")
 n = InputBox("Input an interger n")
 Call GCD(m, n)
 End Sub[/FONT]
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
If you make your variables Public, then you only need to declare them once. I usually list them all in a separate module...
Public m as Integer
Public n as Integer
etc
etc
 
Upvote 0
If you make your variables Public, then you only need to declare them once. I usually list them all in a separate module...
Public m as Integer
Public n as Integer
etc
etc

Your mean Public suit for once variable and Private suit for many time variable?
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,767
Members
449,049
Latest member
greyangel23

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