Access 2013
I'm calling a formula to modify a string and it's changing the values w/in the parent sub.
Example:
Debug.Print Str 'Hello World my name is bob
BOBexists = InStringChceck(Str,"bob")
Debug.Print Str 'HELLO WORLD MY NAME IS BOB
Debug.Print BOBexists 'TRUE
I've used this function, InStringCheck, in Excel VBA before (and it's just an example, all of my string tools are doing this same thing now and I don't know why)
Function InStringCheck(Phrase as string, Term as string) as Boolean
Phrase = UCase(Phrase)
Term = UCase(Term)
if instr(1, Phrase, Term) then InStringCheck = True else InStringCheck = False
end function
In several of my functions I manipulate the input variables, to arrive at a solution, but I don't want those manipulations to persist outside of the function unless I pass them back up - some how they're being passed up, but they're not dimed as public variables
VBA parameters are implicitly passed by reference (ByRef). This means you're passing a reference to the value, not the value itself: mutating that value inside the procedure will result in that mutated value being visible to the calling code.
This is often used as a trick to return multiple values from a function/procedure:
Public Sub DoSomething(ByVal inValue1 As Integer, ByRef outResult1 As Integer, ...)
You have two options:
Pass the parameters by value (ByVal)
Introduce local variables and mutate them instead of mutating the paramters (and heck, pass the parameters ByRef explicitly)
If you have lots of occurrences of parameters being implicitly passed ByRef in your project, fixing them everywhere can easily get tedious. With Rubberduck you can easily locate all occurrences, navigate there, and apply appropriate fixes:
Disclaimer: I'm heavily involved in the Rubberduck project.
Building a little on #Sorcer's answer, VBA has default Sub/Functions parameters passing "by reference" (i. e.: "ByRef" keyword assumed if not specified) so that if you don't want their "inside" modifications survive outside them you have to explicitly type "ByVal" keyword before them in the arguments list.
But you have the option to avoid such modifications take place altoghether by using StrComp():
Function InStringCheck(Phrase as string, Term as string) as Boolean
InStringCheck = StrComp(Phrase, Term, vbTextCompare) = 0
End Function
Which could also lead you to avoid the use of InStringCheck() in favour of a direct use of StrComp() in your code
Related
I have this function within my code:
Function get_header(ByVal rw As Range) As Scripting.Dictionary
Dim header As New Scripting.Dictionary
Dim used As Range
Set used = Range(rw.Cells(1, 1), rw.Cells(1, rw.Cells(1, rw.Columns.Count).End(xlToLeft).Column))
For Each cl In used.Cells
header.Add cl.Value, cl.Column
Next
Set get_header = header
End Function
What the function does is it takes the table header and creates a dictionary of column names and the respective indexes, so that the order of columns is not important for the rest of the code.
My question is: is it necessary to use a separate variable to store the value throughout the loop, or can I
edit the returned value ("get_header") whole time instead of only passing the value at the end or
use the With structure like this:
Function get_header(ByVal rw As Range) As Scripting.Dictionary
Dim used As Range
With rw
Set used = Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
With get_header
For Each cl In used.Cells
.Add cl.Value, cl.Column
Next
End With
End Function
Also, why should I use any of these structures instead of the others?
Thanks for any advice, guys.
Wanted to comment this, but too long for a comment:
When I have a function that "calculates" the return value step by step, I usually prefer to have an intermediate variable for the following reasons (note that using extra variables comes at no cost during runtime, that's not an issue):
Naming: I don't like assigning values to something that is called getSomething.
when used right-sided, it avoids ambiguity between the "intermediate" value and a recursive call to the function: getSomething = getSomething + getSomething(p). The compiler can handle that, but my brain? Not so much. I find retVal = retVal + getSomething(p) is much clearer.
When running on an error condition in the middle of execution, I can easily do an Exit Function without thinking about what was already calculated.
But at the end of the day (and this valid also for the question if of if not to use a With-statement), there are two things that matters: (1) Which code is easier to read and understand. and (2) Which code is less likely to hold an error. As human brains don't work all the same: Find you personal style and stick to it.
I would favor your first example instead of editing the function's return value more than once in a single function call.
Depending on whether error trapping breaks or is silent (i.e. on error resume next), you may get different and unintended results. Importantly, you may get unintended results and not know it.
If a few cycles run fine, and the function result variable is updated a few times, then some error occurs with processing a later cycle (due to inputs, scope, range, etc issues for the later part of the input array, or even just memory or focus or other runtime issues), you will have a problem: the function returns a premature (therefore incorrect) result, but appears as though it has run properly.
I'd recommend coding to avoid this situation, whether or not errors are suppressed. An easy way to do this is to not set the function result object more than once per possible function execution path.
Your first code block seems to conform to this thinking already. Each of your alternate suggestions 1 and 2 do not.
I'm trying to match an obtained property from one class with another class's same property at run time. After some research, I found out that there is such thing as "Reflection" in .net but Im using just VBA. Just for background: I'm automating an application using this code, so some objects are exposed, while others are not.
The way I'm currently doing it is using a property of obtained class "Description", and use that to search for the same property at the targeted class.
Set TargetVar = hySetOperations.Item(j).TargetVariable 'This is a RealVariable a property that refers to a class
Set SourceObj =hySetOperations.Item(j).SourceObject 'This is also a RealVariable
'In order to import variable from source object, we r gonna use TargetVariable description and truncate space, and use it (This might not work if description
'is different than actual name of the variable)
Dim RealVarString As String
RealVarString = TargetVar.Description
'Trim spaces
RealVarString = Replace (RealVarString, " ", "")
Set SourceVar = CallByName ( SourceObj, RealVarString, vbGet)
This actually works for most of the cases since "description" is usually the same as property's name, but with spaces. However, in some cases, this is not the case, in which things go south.
I'm trying to loop through the passed parameters of a function/sub and print their values (for debugging).
The annoying way would be to create an array/collection at the start of each function that loads my declared params in a hardcoded way for each function:
Function myfunc(foo, bar)
Dim fPARAMS As New Collection
fPARAMS.Add foo
fPARAMS.Add bar
'...
Is there is some built-in way to get an array-like object that has my parameters?
Something like;
fPARAMS() = built_in_func_that_returns_my_params()
I haven't found anything on Google nor in SO.
I am writing an Excel VBA program that validates a school course schedule. A key component is a global dictionary object that keeps track of the course number (the key) and the number of times that course is scheduled (the item). I have successfully created and loaded the dictionary. I'm trying to lookup the value associated with the course key, but have been unable to do so using the one-line examples I've found at this site. I'd like to use this line of code:
intCourseCnt = gdicCourses("BAAC 100")
or
intCourseCnt = gdicCourses.Item("BAAC 100")
but neither work (actually, the "BAAC 100" part is a string variable, but it won't even work if I hardcode a course in.) Instead, I have to use the kludgy loop code below to lookup the course count:
Private Function Check_Course_Dup_Helper(strCourse As String) As Boolean
Dim k As Variant
Check_Course_Dup_Helper = False
' Read thru dictionary. Look to see if only 1 occurrence then jump out.
For Each k In gdicCourses.Keys
If k = strCourse Then
If gdicCourses.Item(k) = 1 Then
Check_Course_Dup_Helper = True
Exit Function
End If
Exit Function
End If
Next
End Function
Is there a way to rewrite this so that I can lookup of the item value without the loop?
Thank you.
Thanks for the prompt replies. Answers below:
David, the gdicCourses("BAAC 100") code value while the program is running is "empty" which makes the receiving variable equal to 0. The result is the same if I use strCourse variable. Also, the dictionary populating code is shown below. I do not believe it is a problem because I can correctly access the values elsewhere in the program where For-Each-Next loops that use a range variable are employed. Whitespace and non-printable characters are not present.
My guess is that I need to use a range to reference the position in the dictionary rather than a string. I've tried pretty much every combination of this that I can think of, but the value is still "empty".
Set gdicCourses = New Scripting.Dictionary
For Each c In Worksheets("Tables").Range("combined_courses").Cells
If Not (gdicCourses.Exists(c)) Then
gdicCourses.Add c, (Application.WorksheetFunction.CountIF(Range("MWF_Table_Full"), c
(Application.WorksheetFunction.CountIf(Range("TTh_Table_Full"), c)))
End If
Next
How can I reference a user defined type using a local variable without creating a copy of the type instance?
As an example, in the code below what I would ideally like to do is in MySub3 where I create a local variable, MT, and reference a data structure nested inside another struct ... but VBA doesn't allow this. It allows it for objects but not for user defined types (arrggg!) ... and for no apparent reason ... it just doesn't allow it.
MySub1 shows how to reference the nested struct in a long clunky way.
MySub2 shows how to do this by passing in the nested struct, but this clutters up the calling routine, and having multiple such nested structs gets ugly.
MySub2 demonstrates that VBA can do what I want, it just doesn't seem to provide a way to do it. I'm hoping there is a method I just haven't stumbled upon.
Note that my actual code is MUCH more complicated than this example, with multiple independent structs providing indices to many arrays as struct elements. Using these local reference variables would make the code much more readable and manageable.
Also Note that I am aware of the "with" statement, and it does help, but can only be used on one struct at a time.
Also Note that I am aware that I could use an actual object class. My code started out using an object but I quickly found out that VBA places limitations on arrays as property members ... a limitation that user defined types don't have.
Type tMyType
VariableA As Single
End Type
Type tMyOtherType
MyTypeArray() As tMyType
End Type
Type tOneMoreType
MyOtherType As tMyOtherType
End Type
Dim GlobalIndex As Integer
Sub TopLevel()
Dim TopLevelType As tOneMoreType
ReDim TopLevelType.MyOtherType.MyTypeArray(0 To 10)
Call MySub1(TopLevelType)
Call MySub2(TopLevelType.MyOtherType.MyTypeArray(GlobalIndex))
Call MySub3(TopLevelType)
End Sub
Sub MySub1(OMT As tOneMoreType)
Dim VarA As Single
VarA = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA
End Sub
Sub MySub2(MT As tMyType)
Dim VarA As Single
VarA = MT.VariableA
End Sub
Sub MySub3(OMT As tOneMoreType)
Dim VarA As Single
Dim MT
Set MT = OMT.MyOtherType.MyTypeArray(GlobalIndex)
VarA = MT.VariableA
End Sub
From my point of view you have made it vary complicated. But I believe you have the reason for that.
The example you submitted generate the error you mentioned. But, when I changed some lines there is no error. I am not sure if my suggestion is the result you expected (while the question isn't fully clear to me) but try this instead of your MySub3:
Sub MySub3(OMT As tOneMoreType)
Dim VarA As Single
Dim MT
MT = OMT.MyOtherType.MyTypeArray(GlobalIndex).VariableA
VarA = MT
End Sub
Generally, this way I'm able to read any element im MySub3 passed from TopLevel.
If it is not the answer please clarify more.
I think here you have hit one of the limitations of VBA. I know of no way round the limitation on partial dereferencing of nested user types.
I think you would be best using classes containing private arrays with getter and setter functions (sadly, VBA doesn't have operator overloading either).