0
Function Check() As Boolean

    Check = True

End Function


Private Sub CommandButton1_Click()
    
    dim a as Boolean = Check()
    
End Sub  

I was trying to make a bool function that checked each textbox and gave me back a true/false value with msgbox, but when i tried to call the function it gave me a compilation error: expected various, i tried to put it public but nothing changed.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
MrAlex01
  • 17
  • 5

2 Answers2

1

In VBA, you can't declare and assign in the same command like that, you would do this instead:

Function Check() As Boolean

    Check = True

End Function


Private Sub CommandButton1_Click()
    
    Dim a As Boolean
    a = Check()
    
End Sub
CLR
  • 11,284
  • 1
  • 11
  • 29
-1

The error you are getting is because the Check() function is not declared as a Public function. In order to call a function from another procedure, it must be declared as Public.

To fix this, you need to change the declaration of the Check() function to the following:

Public Function Check() As Boolean

Check = True

End Function Once you have made this change, the function should be able to be called from the CommandButton1_Click() procedure.

Here is the complete code:

Function Check() As Boolean

Check = True

End Function

Private Sub CommandButton1_Click()

Dim a As Boolean
a = Check()

End Sub This code will create a Check() function that returns a Boolean value of True. The CommandButton1_Click() procedure will then call the Check() function and store the result in the a variable.

To test the code, you can run the macro by pressing F5. The Check() function will return a value of True, and the a variable will be assigned the value of True.

connecttopawan
  • 208
  • 3
  • 14