VBA Excel "Variables are required" - vb.net

Got a trouble in a VBA excel program.
Sub code(s)
...
code = t
End Sub
And then :
Sub CommandButton1_Click()
...
For i = 0 To size
current_y = code(string_array(i))
...
End Sub
When i run the program, i got this error "Variables are required" (not sure, i'm working on a japanese version of excel). Sub CommandButton1_Click is highlighted and code is selected inside CommandButton1_Click. Can't figure out why, though it must be simple...

You're trying to return a result from a Sub. Try declaring it as a function instead, as this is able to return values to the caller:
Function code(s)
...
code = t
End Function
If it makes it any clearer, on my English version the error message is:
Expected Function or variable

Does the code include Option Explicit? Perhaps the error translates to "Variable declaration required"? Try removing option explicit - if that fixes it remove that line, then go through checking all variables are declare (e.g. dim current_y as string).

Related

Filter Function not working or recognized in VBA Access

I am trying to filter an array in VBA by using the filter function. But every time I try to execute the code, I get an error message
Compile error:
Wrong number of arguments or invalid property assign
for some reason. I copied some code from an external source to test it out, but even the simplest example doesn't seem to work. Here's the code:
Private Sub Befehl0_Click()
Dim langs(5) As Variant
langs(0) = "English"
langs(1) = 375
langs(2) = "Spanish"
langs(3) = 442
langs(4) = "Chinese"
langs(5) = 1299.5
output_arr = Filter(langs, 375)
End Sub
Every time I execute the code, the function "Filter" gets highlighted. Does anyone know the answer to this problem?
It works if I try output_arr = VBA.Filter(langs, 375) as BigBen has stated in the comments!

Runtime error 438 on remove dynamically added userform control

I've got the following code:
Private Sub cboA_change()
'Something to determine number of controls there should be, variable gC
'Something to determine number of controls there are, variable nC
'The first time the code runs, the following code runs:
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
'The second time the code runs, the following is executed:
For i = 7 To nC
Me.Frame1.Controls("txtGroup" & i).Remove 'ERROR HERE
Next
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
End Sub
Something like this, the code is way bigger and I tried to clear it up so if the structure doesn't seem right, that doesn't matter really.
I debugged the Add statement and I know there is a control added to the userform, called txtGroup7. However, when I later try to remove this control, I get Run-time Error 438: Object Doesn't Support This Property or Method. I tried changing the code to:
Me.Frame1.Controls.Remove ("txtGroup" & i)
But this didn't work either.
Can anybody point me in the right direction?
Edit:
I know the help says the following:
"This method deletes any control that was added at run time. However,
attempting to delete a control that was added at design time will
result in an error."
But since the control is added in run-time (dynamically, with VBA code) this shouldn't be a problem, right?
Edit 2:
I don't get why this works, but it seems to work:
q=0
While q < Me.Frame1.Controls.Count
If Me.Frame1.Controls(q).Name = "txtGroup7" Then
Me.Frame1.Controls.Remove q
Else
q = q + 1
End If
Wend
You must have something else wrong in your code, because Removeshould be working. Tried :
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
Me.Frame1.Controls.Add "Forms.TextBox.1", "Text1", True
Else
Me.Frame1.Controls.Remove "Text1"
End If
End Sub
on a UserForm with a ToggleButton and a Frame and it correctly add and remove the TextBox when pressed.

Excel VBA: Runtime Error 424, Object Required

I can't figure this issue out. I had a similar problem before, it turned out I was using a Range of a single cell, which was redundant. But in this case I need End(xlDown) and I can't get it to work. I tried a few combinations and I can't figure out the right syntax. Help?
Public Exceptions As Range
Public Xcept As Range
Sub Example()
Static ExcSh As Worksheet
Set ExcSh = Worksheets("ComboExceptions")
Set Exceptions = ExcSh.Range("A2")
Set Xcept = ExcSh.Range(Exceptions.Offset(1).Cells, Exceptions.Offset(1).Cells.End(xlDown))
'This is where the error happens ^
End Sub
Solved it! I had declared
Static ExcSh As Worksheet
in another Sub making it inaccessible to the function that errored. I made it public and now the following command works fine:
Set Xcept = ExcSh.Range(Exceptions.Offset(1), Exceptions.Offset(1).End(xlDown))

Excel VBA Procedure Error

I'm having a peculiar issue with a VBA assignment for school. The issue I'm having is VBA is giving me a compile error when Excel tries running the code at start up on a specific Workbook. It keeps telling me that the code cannot be run outside a procedure, but I don't know why it's telling me that when it needs to run at the start of the document. Here's my almost final code that's throwing the error...
Option Explicit
Worksheets("StartPage").Activate
Worksheets("Payroll").Protected
cmdDisplay.Enabled = False
cmdEmpData.Enabled = False
cmdEmployees.Enabled = True
cmdReset.Enabled = True
Public intNumEmp As Integer
That code runs BEFORE the first subroutine is run, which, aside from the variable, is throwing the error. Should I put an access modifier before them to fix the issue, or is there something else I'm missing?
#RonRosenfeld is correct, here is what the code would look like inside of the ThisWorkbook module:
Option Explicit
Public intNumEmp As Integer
Private Sub Workbook_Open()
Worksheets("StartPage").Activate
Worksheets("Payroll").Protected
cmdDisplay.Enabled = False
cmdEmpData.Enabled = False
cmdEmployees.Enabled = True
cmdReset.Enabled = True
End Sub

Does VB.NET have an empty statement?

I am looking for an empty statement in VB.NET similar to this in C/C++:
;
My understanding is this is simply an empty statement that does nothing. However, when I try to use a C# to VB.NET converter to gain some insight I am just given a blank line. Is there something similar I can use besides empty space?
When I have to do a do-nothing line in VB.NET just like "pass" in Python, I use the following one liners:
The "Just 8-bits Used"
Dim i As Byte = 0
"The Shortest One"
Dim i = 0
The "No Memory Used"
If 1 <> 1 Then Dim i = 0
I suggest you to add a "TODO / HACK / UNDONE" comment so you can remember this line it's just a placeholder line and you can find it easily in your task list. Example:
' TODO: Add function logic
Dim i = 0
More info about Creating task list comments HERE
In C an expression becomes a statement when it is followed by a semicolon. The semicolon itself is just a statement terminator. The equivalent to a statement terminator in Visual Basic is a line break and the equivalent of an empty statement is an empty expression followed by a line break.
Your first example would look something like this.
Function ProcessMessage() As Boolean
...
End Function
Sub ProcessMessages()
While ProcessMessage()
End While
End Sub
Your second example would look something like this. Note however that exit is a keyword in Visual Basic, so we have to call the label something else.
Sub f()
...
If done Then
GoTo _exit
End If
...
_exit:
End Sub
VB.Net does not have an empty statement, but you can easily add one (tested with .net 6.0):
Imports System.Runtime.CompilerServices
public Module VbExtras
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Sub NextSentence() : End Sub
End Module
...
Public Class C1
Public function Method1() as Boolean
If l > 0 Then
If b IsNot Nothing Then NextSentence '' useful for precompiler work
Else
Console.WriteLine("filled")
End If
return False
End Function
End Class