In a sub I've made in VBA, which is executed everytime the worksheet changes, the sub calls a function to determine if the user has deleted or changed something they shouldn't have. The function returns a boolean value to tell the sub if such a critical value has been deleted/edited. This way, the sub knows not to continue executing code.
However, whenever the code executes, VBA returns a Compile Error: Expected array error for the calling of the criticalDataIntact() function, despite there being no arrays used.
Here's my relevant code,
Private Sub Worksheet_Change(ByVal target As Range)
Worksheets(CONFIG).usedRange 'Refresh UsedRange
Dim criticalDataIntact As Boolean: criticalDataIntact = criticalDataIntact()
If Not criticalDataIntact Then
Exit Sub
End If
'Irrelevant code
End Sub
Private Function criticalDataIntact() As Boolean
Dim criticalDataIntact As Boolean: criticalDataIntact = True
Set warnWorkloadCell = ThisWorkbook.cell(CONFIG, WARNING_WORKLOAD, 0, 0)
Set dangerWorkloadCell = ThisWorkbook.cell(CONFIG, DANGER_WORKLOAD, 0, 0)
Dim table3Exists As Boolean: table3Exists = tableExists("Table3")
If warnWorkloadCell = Null Or dangerWorkloadCell = Null Or table3Exists = False Then
criticalDataIntact = False
End If
End Function
The cell function outputs an error message via MsgBox and returns Null if it couldn't find a cell with a specific value in it within a specific worksheet.
Any ideas?
Returnkeyword, but that got aExpected: end of statementerror. According to Dan on stackoverflow.com/questions/2781689/…, the way I've done it is the correct way.returnto pass function results back to the calling procedure. That's a ,NET framework way to do things (VB.NET). Instead, you set the name of the function to the result to be passed back (returned) at the end of the function code. In contrast to QHarr's last remark I prefer/recommend a local variable and passing back at the end. That's clearer and less prone to errors (in my opinion).