Compile error "expected: end of statement" - excel

I have the following calcScores function written:
Function calcScores(category As String) As Integer
Dim count As Integer
count = 0
For Each Ctl In UserForm1.Controls
If Ctl.Tag = category And TypeName(Ctl) = "CheckBox" Then
Dim box As MSForms.CheckBox
Set box = Ctl
If box.Value = True Then
count = count + 1
End If
End If
Next
calcScores = count
End Function
This function takes a tag named "category" as a string and then checks the form for all check boxes with that tag and counts the ones that are checked. I know it works and counts the right number, because I have slightly edited it to output it's value to a label on the form instead of returning it.
When I try to call it in another function like this:
Function sortScores()
Dim scores(0 to 5) as Integer
scores(0) = calcScores "rChk"
**CODE CONTINUES**
End Function
I get an error that says "Expected: End of Statement" as soon as I leave the line that assigns the function's return to scores(0). calcScores is assigned before sortScores, and was succesfully called in a sub before using the same syntax.
Any idea what the error could be?

Call you function like this
scores(0) = calcScores("rChk")
Functions are called like that. Subs are called by
subName argument

Related

VBA Excel: How to put classes into an array

Is there anyway that I can put various classes into an array so that instead of hard-coding like:
set var1 = new cls1
set var2 = new cls2
we can do:
varArr = array ( var1, var2...)
clsArr = array (cls1, cls2...)
and now I just loop thru varArr and clsArr to set new class accordingly, like:
sub setNew(x as double)
set varArr(x) = new clsArr(x)
end sub
I did try but it didn't work, please help me out
Thank you very much !
Triet (mr)
I try, but my knowledge also limited.
First of all I do not think you can put class into an array. You can put objects. I also failed to use the array() function.
Please see how I proceeded with some demonstration:
This is the class module, containing a value, the property and a primitive print sub:
Private nr As Integer 'Use as identification
Sub cPrint()
MsgBox "Object ID: " & nr
End Sub
Property Let ID(v As Integer)
nr = v
End Property
In the module in the first cycle I create the object, put the ID value. In the second one all ID values are printed. I hope you can use it for your purposes.
Sub clsTest()
Dim varArr(1 To 5) As cls1
Dim i As Integer
For i = 1 To 5
Set varArr(i) = New cls1
varArr(i).ID = i
Next
For i = 1 To 5
varArr(i).cPrint
Next
End Sub

Get Function() result as Control

Background:
I needed a function to correlate Controls efficiently based on a STD name and the text analyzed. IG:
a) Some other input throws the variable "mytext"
b) If the ListBox1.Value has "mytext" then I have to relate it with ToggleButton1
Approach:
I made the following function which partially works
Code:
Private Function RelateControl_ToggleVsList(ToggleCtrl As Control) As Control
Dim ItemControl As Control
Dim myControl As Object
For Each ItemControl In Me.Controls
If TypeName(ItemControl) = "ListBox" Then ' 1. If TypeName(ItemControl) = "Label"
'text lenghts const 13 for ListBox_TimeXX and 22 for ToggleButton_PriorityXX
If Mid(ItemControl.Name, 13, 2) = Mid(ToggleCtrl.Name, 22, 2) Then Set RelateControl_ToggleVsList = ItemControl: Exit Function
End If ' 1. If TypeName(ItemControl) = "Label"
Next ItemControl
End Function
Problem:
I get a null property when Setting the result:
Set RelateControl_ToggleVsList = ItemControl 'This is nullSet
Debugging process:
Question:
How can I set a Control as a result of this function?
EDIT:
Per request I add the whole Debugging in order to see where it is being called form
Calling Code
Private Sub ToggleButtons_Active()
Dim ItemControl As Control
Dim ItemTextBox As Variant
Dim TxtControl As String
For Each ItemControl In Me.Controls
If TypeName(ItemControl) = "ToggleButton" Then ' 1. If TypeName(ItemControl) = "ToggleButton"
TxtControl = CStr(RelateControl_ToggleVsList(ItemControl).Value)
If InStr(TextBox_Notes.Value, TxtControl) > 0 And TxtControl <> "" Then ItemControl.Value = True
End If ' 1. If TypeName(ItemControl) = "ToggleButton"
Next ItemControl
End Sub
Your error is occurring on your line which says
TxtControl = CStr(RelateControl_ToggleVsList(ItemControl).Value)
because the returned Control's Value property is currently Null which can't be cast to a String.
I recommend that you change TxtControl to be a Variant type, then say
TxtControl = RelateControl_ToggleVsList(ItemControl).Value
If IsNull(TxtControl) Then
TxtControl = ""
Else
TxtControl = CStr(TxtControl)
End If
Or you could define a Control object and then use it:
Dim MyControl As Control
MyControl = RelateControl_ToggleVsList(ItemControl)
If IsNull(MyControl.Value) Then
TxtControl = ""
Else
TxtControl = CStr(MyControl.Value)
End If
Solution:
The object itself is calling a property which does not belong to it (as intended).
ListBox Properties does not show "Value" as its text, the real thing should have called it as:
TxtControl = CStr(RelateControl_ToggleVsList(ItemControl).List(0))CStr(RelateControl_ToggleVsList(ItemControl).List(0))
Furthermore:
Thanks to the solutions provided and the debugging process, I could notice even the object is Set in the function, it is shown as the "value" property of it while debugging.

Type mismatch when trying to assign return value to variable (VBA Excel)

Long time listener, first time caller.
I am having an issue with VBA in Excel 2010. I am trying to compare various fields on a user form to determine if they are empty. I will then highlight red, and set focus on the first one on the list.
To do this, I have created functions for each type that can be blank/not selected. They are all declared AS BOOLEAN. The idea was to do the function call to highlight, pass the function calls with the appropriate sub functions, and use the results of those in the highlight function:
function Blah(DoThis01(Me.Object1), DoThis02(Me.Object2), ...) As Boolean
That gave me nojoy; the values always came back false. So I created three boolean variables to assign the values of those functions, and it started givinng me byRef errors. After declaring each variable rather than all on one line, that solved the byref, but now I get a type mismatch:
Dim foo As Boolean
foo = DoThis01(Me.Object1)
if Blah(foo, bar, ..)
It doesn't like that center line; that is where I am running into the error.
Below is the code snippet I am working with, along with an example of the functions I am trying to pass. I can't figure out why my As Boolean functions aren't compatible with an As Boolean variable. Also, the functions to determine whether or not the fields are filled are giving me some issues as well, not registering the correct values. Google had lots of answers, but none that worked for me so far. I appreciate any insight you can offer.
Thanks,
J
PS I realize some of the logic may be a bit off when triggering the message. That is ok, because I am not getting the right values anyways. I will clean it up if I can get the rest working.
Working Code:
Save Button:
Dim emptyRow As Long
Dim isDuplicate As Boolean, nameSelect As Boolean, comboSelect As Boolean, listSelect As Boolean
Dim listLength As Integer
Dim blah As Integer
' check for empty on form
nameSelect = IsSelectedName(Me.SignalNameTxtBox)
comboSelect = IsSelectedCombo(Me.ComboBox1)
listSelect = IsSelectedList(Me.ListBox1)
If HighlightEmpty(nameSelect, comboSelect, listSelect) Then
blah = MsgBox("Empty fields required!", vbOKOnly)
Exit Sub
End If
...More stuff
To Highlight Empty Form Objects:
Function HighlightEmpty(nameSelect As Boolean, comboSelect As Boolean, listSelect As Boolean) As Boolean
' Highlight empty fields
If Not nameSelect Then Enter_New_Form.SignalNameTxtBox.BackColor = RGB(255, 0, 0)
If Not comboSelect Then Enter_New_Form.ComboBox1.BackColor = RGB(255, 0, 0)
If Not listSelect Then Enter_New_Form.ListBox1.BackColor = RGB(255, 0, 0)
' Set focus to first empty field on form
If Not nameSelect Then
Enter_New_Form.SignalNameTxtBox.SetFocus
ElseIf Not comboSelect Then
Enter_New_Form.ComboBox1.SetFocus
ElseIf Not listSelect Then
Enter_New_Form.ListBox1.SetFocus
End If
' Return boolean to trigger message
HighlightEmpty = nameSelect Or comboSelect Or Not listSelect
End Function
My functions to determine if empty:
Function IsSelectedList(lst As ListBox) As Boolean
Dim I As Integer
For I = 0 To lst.ListCount - 1
IsSelectedList = lst.Selected(I)
If IsSelectedList Then Exit Function
Next I
End Function
Function IsSelectedCombo(cbo As ComboBox) As Boolean
If cbo.Value = "" Then IsSelectedCombo = False
End Function
Function IsSelectedName(nme As TextBox) As Boolean
If nme.Value = "" Then IsSelectedName = False
End Function
Your functions will always return false unless otherwise declared as true. Add an else statement like this:
Function IsSelectedName(nme As TextBox) As Boolean
If nme.Value = "" Then
IsSelectedName = False
Else
IsSelectedName = True
End If
End Function
As #Timwilliams pointed out you can reduce this to one line since nme.Value = "" will evaluate to True or False. You might find however, that you need to test if the cell is empty in other ways such as isBlank() and isEmpty(). In this case use the previous example.
Function IsSelectedName(nme As TextBox) As Boolean
IsSelectedName = Len(nme.Value) > 0
End Function
EDIT
To check the value just pass the text string to the function rather than the textbox object. Try This:
Private Sub CommandButton1_Click()
MsgBox (IsSelectedName(Me.SignalNameTxtBox.Text))
End Sub
Function IsSelectedName(nme As String) As Boolean
IsSelectedName = Len(nme) > 0
End Function

How do I display an empty string when a variable is not initialized?

Im trying to display an empty string which is pretty straight forward, how is their a way to display an empty number for an integer? i have the example below.
Sub()
Dim s As String
Dim Number As Integer
'using a space in double quote
s = " "
'this will display an empty string, well not really empty but the space will make it empty
msgbox(s)
so basically im trying to do the same thing integer whereas when you use a msgbox for it
it displays nothing.
You should make a function for your needs
function ShowNumberNonZero( n ) as string
if n = 0 then
ShowNumberNonZero = ""
else
ShowNumberNonZero = Trim(n)
end if
end function
and then
msgbox(ShowNumberNonZero(Number))
If you don't set your integer at all, calling MsgBox will assign it 0 and it will display 0. The only way to make it so that it will display nothing would be to use an if statement or a function.
It might look something like this:
Private Sub Run()
Dim number As Integer
Message(number)
End Sub
Private Sub Message(ByVal input As Integer)
If input = 0 Then
MsgBox("")
Else
MsgBox(input)
End If
End Sub

Why can't I send a OptionButton a parameter in VBA

I have the following function in VBA:
Private Function Option1Checked(option1 As OptionButton) As Integer
option1.ForeColor = vbGreen
If (option1.Value = True) Then
Option1Checked = 1
End If
Option1Checked = 0
End Function
Whenever I try to call the function like this
counter = counter + Option1Checked(OptionButton1)
I get a type mismatch error at runtime. But OptionButton1 is OptionButton, so what am I doing wrong?
You're running into one of the 'features' of VBA here. If you refer to some objects, like the option button, without a property specified, VBA assumes you want the default property, not the object itself. In the case of the option button, the default property is .Value, so in your code, OptionButton1 is not the option button object, but rather TRUE or FALSE depending on whether or not the OptionButton1 is checked.
Your best bet will be to change your function to this:
Private Function Option1Checked(option1 As Boolean) As Integer
//option1.ForeColor = vbGreen
If (option1 = True) Then
Option1Checked = 1
Else
Option1Checked = 0
End If
End Function
The downside here is that you cannot change the foreground color of the option button to green without referring to it by name.
An alternative that would get you the functionality that you want would be to pass the name of the option button to your Function.
Private Function Option1Checked(ByVal option1 As String) As Integer
UserForm1.Controls(option1).ForeColor = vbGreen
If (UserForm1.Controls(option1) = True) Then
Option1Checked = 1
Else
Option1Checked = 0
End If
End Function
Sub MyCountingRoutine()
Dim str As String
str = OptionButton1.Name
counter = counter + Option1Checked(str)
End Sub
Make sure you include the Else in the If..Then statement in your function, otherwise you will always get 0 back.

Resources