Keep a running count of an integer between Subs

Keep a running count of an integer between Subs

dalton98
Collaborator Collaborator
388 Views
4 Replies
Message 1 of 5

Keep a running count of an integer between Subs

dalton98
Collaborator
Collaborator

Hello,

I'm fairly new to VBA, specifically working with multiple subs and functions. My question is how do you transfer a variable/ integer back to the main sub? Here is an example of what I'm trying to do, but it resets the value "i" every time it enters a new sub.

Public Sub Main
Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument
Dim oBOM As BOM = oAss.ComponentDefinition.BOM
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
Dim oRow As BOMRow
For Each oRow In oBOMView.BOMRows
	i = cellRow(i)
	If Not oRow.ChildRows Is Nothing
		Call oChildRow(oRow, i)
	End If
Next
MessageBox.Show(i)

End Sub

Public Sub oChildRow(oRow As BOMRow, i As Integer)
	For Each oRow In oRow.ChildRows
		i = cellRow(i)
		If Not oRow.ChildRows Is Nothing
			Call oChildRow(oRow, i)
		End If
	Next
End Sub

Public Function cellRow(i As Integer)
	i = i + 1
	Return i
End Function

 

0 Likes
Accepted solutions (1)
389 Views
4 Replies
Replies (4)
Message 2 of 5

MTheDesigner
Advocate
Advocate

I think this is your problem.

For Each oRow In oBOMView.BOMRows
	i = cellRow(i)

 You are reinitializing the value i every for loop. 

if you want i to remember its value outside of the loop you need to initialize it before the loop

 

Dim i As integer
For
Each oRow In oBOMView.BOMRows i = cellRow(i)

 

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @dalton98.

  • First of all, the code you have posted here appears to me to be either iLogic or vb.net, instead of VBA.  I say that because you are setting the values of variables on the same line as you are originally declaring the variables, and not using the keyword Set on the next line to set the values of your variables.
  • Next, there are a couple of terms you may need to familiarize yourself with, when using Sub routines and Function routines.  Use the keywork 'ByVal' (aka ReadOnly) before the name of a variable in the definition line of a Sub routine to only pass the Value of that variable to the Sub routine, and that Sub routine will not be able to change the value of the source/original variable in the main routine that called it to run.  Or use the keyword 'ByRef' (aka Read/Write) before the name of a variable when you want the Sub routine to be able to change the value of the source/original variable in the main routine that called it to run.
  • Next, a Sub routine is meant to just do something, without returning anything to the main routine that called it to run, while a Function routine IS meant to return something to the main routine that called it to run.  So, at the end of your definition line of a Function routine, you should have the keyword 'As', followed by a data type, similar to when you declare a variable and set its Type.  That extra little bit at the end of its definition line tells the calling routine what type of object/data the Function is supposed to return.  Then one option for specifying a value for the function to return is ... somewhere within the Function routine, you should have the keyword 'Return' followed by a variable or result that is the specific type of data that you defined the function to return.  The other option for specifying a value to return, is to use the name of the function just like you would a variable, and set its value to a variable or result of that specific type.  Doing it that way does not cause the Function to immediately end though, so you may need to use an additional phrase 'Exit Function' on the next line, if you want it to maintain that value.  This second option is the proper way when doing it in VBA, by the way.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

dalton98
Collaborator
Collaborator

@WCrihfield Thank you that fixed it. I still have to use the "ByRef" in the sub sub routine even if I use a function to change the variable. Is this intended? From what you said it seems like sub variables should always be in read only.

0 Likes
Message 5 of 5

WCrihfield
Mentor
Mentor

Yes.  That's one of the odd things I picked up on when I was learning this stuff the hard way myself.  You can actually use a Sub routine instead of a Function, when all you want to do is pass a variable down into a routine, let that routine change the value of that variable then those changes will be reflected in the source variable in the main routine when its done...if I used that ByRef keyword when setting up that variable in the definition line of the Sub.  Sometimes that works out OK for what you may need, and other times the way a Function works just makes more practical sense.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes