Excel (Office 365) restarts automatically while executing UDF - vba

Thanks in advance for your help.
I am not sure where the problem in my UDF but the excel restarts automatically after executing the program and also shows MsxBox twice with the reference value.
Public queryString As String
Public Function SetIt(RefCell) As String
On Error GoTo CatchBlock
MsgBox RefCell.Value
queryString = RefCell.Value
RefCell.Parent.Evaluate "SetValue(" & RefCell.Address(False, False) & ")"
GoTo Finally
CatchBlock:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error in Function"
Finally:
SetIt = ""
End Function
// For now I am trying to set the cell reference value in some other cell.
Sub SetValue(RefCell As Range)
RefCell.Offset(1, 1).Value = queryString
End Sub
I am still beginner and need to accomplish another big task based on this program code execution.
Please help !!
Excel 64 bit & Office 365 is my environment

Related

MS Access filtering for date causes Error 3075

I have a text field, called DateSelector that is formatted as a Short Date, and a subform with a list of entries, one column named ControlDate contains values that are formatted as Short Date as well.
What I want:
If you change the value of DateSelector it is supposed to filter the column ControlDate to between now and the value of DateSelector, or at least be below that of DateSelector.
This is the code I have:
Private Sub DateSelector_AfterUpdate()
On Error GoTo Proc_Error
If Me.DateSelector.Value = "" Then
Me.ListView.Form.filter = ""
Me.ListView.Form.FilterOn = False
Else
MsgBox (Me.DateSelector.Value)
'This is a Check, if the Value is correct.
Me.ListView.Form.filter = "ControlDate >=" & Me.DateSelector.Value
Me.ListView.Form.FilterOn = True
End If
Proc_Exit:
Exit Sub
Proc_Error:
MsgBox "Error " & Err.Number & " when creating Filter:" & vbCrLf & Err.Description
Resume Proc_Exit
End Sub
The error thrown is 3075: Syntax Error. The MsgBox shows the correct date, where the error message shows the date missing the last digit (e.g.: 05.12.2018 --> 05.12.201) and I have absolutely no clue why.
I am thankfull for any answers, thank you for reading,
_Ninsa
You filter will end up as:
"ControlDate >= 01.12.2018"
which Access cannot read. So, apply a proper format of the string expression of the date value:
"ControlDate >= #" & Format(Me!DateSelector.Value, "yyyy\/mm\/dd") & "#"

VBA code in Excel, Argument not optional, CountIf Error

I created a code to find instances of the number 1 in column A in Sheet27 but it keeps giving the error 'argument not optional' and highlighting the CountIf part of the code. I want this information to be displayed as a msgbox.
Private Sub CommandButton1_Click()
Dim instances As Long
instances = WorksheetFunction.CountIf(Sheets("Sheet27")(Columns("A:A"), "1"))
MsgBox "We Found " & instances & " instances of ", vbInformation, "Alert"
End Sub
Try...
instances = WorksheetFunction.CountIf(Sheets("Sheet27").Columns("A:A"), "1")

Simple code password retry

I am a beginner and I have spent hours in Google but no one has the answer.
To test for error write two any password to cause an error – It works OK the first time (Strike 1), but it fails on the second bad password. It’s driving me bananas! I tried to reset the error counter??? – Nothing works for me!!!
I have a workbook open with a routine on “ThisWorkbook” with a call (Branch) to “Sub Password_Check” on Module 9. I am an old Fortran programer but VBA is by far more complicated!
Sub Password_Check()
Dim i As Integer
iTotal = 0
On Error GoTo Message
For i = 1 To 3
ActiveSheet.Unprotect
Message:
If i = 1 Then MsgBox " STRIKE " & (i)
If i = 2 Then MsgBox " STRIKE " & (i)
If i = 3 Then MsgBox " STRIKE " & (i)
Next
Application.Quit
End Sub
I will be very grateful!
Regards,
Oscar G Duarte
Thank you in advance for all your help!!!
Your error is inside the for loop. You want to give the user three tries to unlock a workbook. You need to have the error handler outside of the loop. I'm not a VBA programmer or windows user so I have no way to test this but your logic needs to look something more like this. Hopefully it helps. Look into error handling and subroutines within the context of VBA.
Sub Password_Check()
Dim i As Integer
iTotal = 0
On Error GoTo ErrorHandler
For i = 1 To 3
ActiveSheet.Unprotect
Next
Exit Sub
ErrorHandler:
MsgBox " STRIKE " & (i)
if i = 3 Then
Application.Quit
Else
Resume
End Sub

Combo box getting "Enter Parameter Value" prompt when clicking a button

Any ideas why I am getting an "Enter Parameter Value" input box when running this code?
Private Sub cmdPrint_Click()
Dim str As String
On Error GoTo ErrHandler
If IsNull(Me.Combo_1) Then
MsgBox "Can't print an unsaved record", _
vbOKOnly, "Error"
Exit Sub
End If
str = "Combo_1 = '" & Me!Combo_1 & "'"
Debug.Print str
DoCmd.OpenReport "rptBarCodeLabels(2)", acViewPreview, , str
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " _
& Err.Description, vbOKOnly, "Error"
End Sub
Edit: The button is being used to print a label of what's currently selected in Combo_1. Once the print button has been clicked, I wanted it to display the single record I chose in the label report I have it referencing. I am using Access 2003 if that means anything.
If a field name in a query contains punctuation (Combo_1), you should enclose in brackets, like so:
str = "[Combo_1] = '" & Me!Combo_1 & "'"
The Report is expecting a parameter, but not getting it because it's not being passed through in the correct manner.
I've found a solution by using some coding that was provided here: http://www.techrepublic.com/article/how-to-print-one-or-more-labels-for-a-single-access-record/
What fixed the error was most likely creating a temporary table and temporary report.

VBA "Compile Error: Label not defined"

I have a VBA macro which gave me that error message.
Sub Function1()
' Give the user macro options based on how fast or slow the computer
' is using advanced conditional compiling
vuserChoice = MsgBox("This macro by default treats all numbers as decimals for maximum precision. If you are running this macro on an old computer, you may want to declare numbers as singles, to speed up the macro.")
MsgBox ("Decimal: recommended for maximum precision. Also slower." & vbNewLine & "Long: not recommended. Rounds to nearest integer." & vbNewLine & "Single: not recommended. A lightweight double." & vbNewLine & "Integer: not recommended. Quick and low-precision.")
If vuserChoice = "Decimal" Or "decimal" Then
GoTo FunctionDecimal
ElseIf vuserChoice = "Double" Or "double" Then
GoTo FunctionDouble
ElseIf vuserChoice = "Single" Or "single" Then
GoTo FunctionSingle
ElseIf vuserChoice = "Long" Or "long" Then
GoTo FunctionLong
Else
GoTo FunctionNotValidVarType
End If
' MEeff = measure of efflux due to crudely purified HDL in scintillation
MsgBox "For additional information about this macro:" & vbNewLine & "1. Go to tab Developer" & vbNewLine & "2. Select Visual Basic or Macro." & vbNewLine & "See the comments or MsgBoxes (message boxes)."
End Sub
The offending line is:
GoTo FunctionNotValidVarType
I have the function FunctionNotValidVarType below this code. I have it as:
Public Sub FunctionNotValidVarType()
MsgBox "VarType " & VarType & " is not supported. Please check spelling."
End Sub
What do I need to do to let the first function recognize FunctionNotValidVarType? Thanks.
GoTo will try and transfer the code execution to a different position in the current Subroutine with the given label.
Specifically, GoTo FunctionNotValidVarType will try and execute the line:
FunctionNotValidVarType: 'Do stuff here
which doesn't exist in your current code.
If you want to call another function use Call FunctionNotValidVarType
Remove the word GoTo
GoTo tells the code to jump to a label, you want it to enter a new procedure, not go to a label
Remove Goto from the call to your Sub()
If you really wanted to use a Goto (and you shouldn't), you would
goto Label
Label:
where the label is defined by the trailing colon :
GoTo transitions to a label, a label is defined with :
For example:
Sub G()
On Error GoTo err_handling
a=1/0
Exit Sub
err_handling:
MsgBox "Holy Shit, an error occurred !"
End Sub
To apply GoTo on a Sub you need call it and exit:
Call FunctionNotValidVarType
Exit Sub
(Technically, it is not the same as GoTo if you take the call stack into consideration, but the end result is the same)
GoTo is not considered a good practice, but if that doesn't concern you, take a look also at GoSub at the official docs.