View Full Version : Can I “include” another file or module in a sub or function?
dennygibson
04-25-2019, 02:44 PM
Back to fortran days ... you can create a file with information you want shared with several subroutines by simple adding “include filename” ... very handy when you have a tone of variables to pass to subroutines making the call statement ridiculously long.
Is there an analogous command in VBA?
Paul_Hossler
04-25-2019, 06:48 PM
1. You could make them module level variables
2. Create your own user Type, create a variable of the type, and pass it (you could also use a variant array but a UDT is cleaner IMHO)
Example of the first way
Option Explicit
Dim X As Long, Y As Long
Sub Top()
One
MsgBox X & " -- " & Y
Two
MsgBox X & " -- " & Y
End Sub
Sub One()
X = 100
Y = 200
End Sub
Sub Two()
X = 10
Y = 20
End Sub
Example of the second
Option Explicit
Type MyType
L As Long
S As String
B As Boolean
End Type
Sub One()
Dim X As MyType
With X
.L = 100
.S = "Some Text"
.B = True
End With
Call Two(X)
MsgBox X.L
MsgBox X.S
MsgBox X.B
End Sub
Sub Two(M As MyType)
With M
MsgBox .L
MsgBox .S
MsgBox .B
.L = 2 * .L
.S = .S & " some more"
.B = Not .B
End With
End Sub
dennygibson
04-25-2019, 08:43 PM
Paul ... thanks! Would this work with a list of declarations:
Dim x0, y0, z0, a0, b0, c0 As Double
Dim x1, y1, z1, a1, b1, c1 As Double
Dim x2, y2, z2, a2, b2, c2 As Double
Dim x3, y3, z, a3, b3, c3 As Double
This would be a very short list … the code has a lot of variables that need to be passed to several subroutines. The calls with arguments are several lines long. There must be a better way of making this information available to other subroutines, Something like the include and common functions in fortran.
Bob Phillips
04-26-2019, 01:46 AM
You could also use arrays
Sub One()
Dim X(1 To 3) As Variant
X(1) = 100
X(2) = "Some Text"
X(3) = True
Call Two(X)
MsgBox X(1)
MsgBox X(2)
MsgBox X(3)
End Sub
Sub Two(ByRef M As Variant)
MsgBox M(1)
MsgBox M(2)
MsgBox M(3)
M(1) = 2 * M(1)
M(2) = M(2) & " some more"
M(3) = Not M(3)
End Sub
Or a dctionary, collection, arraylist, sortedlist, combobox. listbox
Sub M_snb()
Set dic1 = CreateObject("scripting.dictionary")
For j = 1 To 236
dic1.Item("Q_" & Format(j, "000")) = j
Next
M_snb_dict dic1
End Sub
Sub M_snb_dict(d00)
MsgBox d00.Count & vbLf & d00("Q_057")
End Sub
Paul_Hossler
04-26-2019, 06:48 AM
Paul ... thanks! Would this work with a list of declarations:
Dim x0, y0, z0, a0, b0, c0 As Double
Dim x1, y1, z1, a1, b1, c1 As Double
Dim x2, y2, z2, a2, b2, c2 As Double
Dim x3, y3, z, a3, b3, c3 As Double
This would be a very short list … the code has a lot of variables that need to be passed to several subroutines. The calls with arguments are several lines long. There must be a better way of making this information available to other subroutines, Something like the include and common functions in fortran.
Your parameters seem to be pretty structured
If it were me, I'd use the UDT approach since I like the Intellisense prompting. You could also use arrays which can be easily looped through if necessary
This is something I did for an astronomy project
Type SphericalCoord
R As Double
Theta As Double
Phi As Double
End Type
Type RectangularCoord
X As Double
Y As Double
Z As Double
End Type
Using your example, Sub Test uses a UDT made up of another UDT x 4
Sub Test2 passes an array
Type XYZABC
X As Double
Y As Double
Z As Double
A As Double
B As Double
C As Double
End Type
Type XYZABC4
n0 As XYZABC
n1 As XYZABC
n2 As XYZABC
n3 As XYZABC
End Type
' or a combined approach
'Dim MyVar (1 To 4) as XYZABC
'so
' MyVar(2).C = 1234
'and just pass Myvar
Sub Test()
Dim T As XYZABC4
T.n0.a = 123
Call LowerLevel(T)
End Sub
Sub Test2()
Dim ArrayApproach(1 To 4, 1 To 6) As Double
ArrayApproach(1, 1) = 123
Call LowerLevel(ArrayApproach)
End Sub
dennygibson
04-26-2019, 08:19 AM
Thank you!
dennygibson
04-26-2019, 08:21 AM
Paul ... thanks!
dennygibson
04-26-2019, 08:27 AM
Thanks!
Bob Phillips
04-26-2019, 11:26 AM
Your parameters seem to be pretty structured
If it were me, I'd use the UDT approach since I like the Intellisense prompting.
Not withstanding that I added arrays, I would either go a UDT or a class as well.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.