I am stuck on local vs global variables. If you call a variable within a sub as "sub test(a as Integer)" is a now a global variable?
I have simplified a lot of code to the below to demonstrate the issue. Right now I have put in another IF statement using the tmp variable to flip cl_min and cl_max back, but would not have thought it would work this way. At first I was using cl_min instead of c_min in create_diagonal so thought a name change would help, but it changes it anyway. Any advice on how to better code this would be appreciated. Thanks!
I have simplified a lot of code to the below to demonstrate the issue. Right now I have put in another IF statement using the tmp variable to flip cl_min and cl_max back, but would not have thought it would work this way. At first I was using cl_min instead of c_min in create_diagonal so thought a name change would help, but it changes it anyway. Any advice on how to better code this would be appreciated. Thanks!
Code:
Option Explicit
Sub Setup_Test_Document()
Dim rack_sample_pattern As String
Dim rw_min As Integer, rw_max As Integer, cl_min As Integer, cl_max As Integer
rack_sample_pattern = "x"
cl_min = 2
cl_max = 11
rw_min = 5
rw_max = 12
MsgBox ("cl_min= " & cl_min & Chr(10) & "cl_max= " & cl_max)
Call rack_sampling(rack_sample_pattern, rw_min, rw_max, cl_min, cl_max)
MsgBox ("cl_min= " & cl_min & Chr(10) & "cl_max= " & cl_max)
End Sub
Sub rack_sampling(rack_sample_pattern, rw_min As Integer, rw_max As Integer, cl_min As Integer, cl_max As Integer)
Call create_x(cl_min, cl_max, rw_min, rw_max)
End Sub
Sub create_x(cl_min As Integer, cl_max As Integer, rw_min As Integer, rw_max As Integer)
Call create_diagonal(True, cl_min, cl_max, rw_min, rw_max)
Call create_diagonal(False, cl_min, cl_max, rw_min, rw_max)
End Sub
Sub create_diagonal(top_left As Boolean, c_min As Integer, c_max As Integer, r_min As Integer, r_max As Integer)
Dim rw, cl, cl_step, tmp As Integer
Dim double_here As Boolean
If top_left Then
cl_step = 1
Else
cl_step = -1
tmp = c_min
c_min = c_max
c_max = tmp
End If
'do work
End Sub