Reputation: 123
In my Main
procedure I want to write a quick if-statement which checks whether the user has made a valid input (user chooses number of project from list of data, see attached screenshot). For that I am checking whether the project number is not part of the list of projects. If that is true, an error message is displayed; if not then a number of other procedures are called.
For some reason though I get error 2015 when I run it, which means that the if-statement is always true, even on correct user entries. Can someone help me understand the error please?
Code below (note: I have tried pasting it 5 times but the formatting still won't work this time for some reason - any idea what that could be? The code is properly formatted. Sorry for the messy display; if anyone can tell me what that problem might I would be very grateful!)
Sub Main()
'Turn off screen updating
Application.ScreenUpdating = False
'Define variable for currently active cell to reactivate it afterwards
Dim OldActiveSheet As Object
Dim OldActiveCell As Object
Dim i As Integer
Dim ProjectList As Range
Set OldActiveSheet = ActiveSheet
Set OldActiveCell = ActiveCell
'If-statement to check whether project number is valid or not
Worksheets("Invoice").Activate
'Print to Immediate Window to check value - remove later
Debug.Print Range("IdSelect").Value
If IsError(Application.Match(Range("IdSelect").Value, "ProjectList", 0)) Then
'Print to Immediate Window to check value - remove later
Debug.Print Application.Match(Range("IdSelect").Value, Worksheets("Input").Range("ProjectList"), 0)
MsgBox "Invalid Choice: Project with this number does not exist!"
Exit Sub
Else
'Call procedures to execute
Call SortData
Call Count_Line_Items
Call Count_Total_Rows
Call Write_Services(ServCnt)
Call Write_Expenses(ExpCnt)
End If
'Reactivate previous active cell
OldActiveSheet.Activate
OldActiveCell.Activate
End Sub
Screenshot from "Input" sheet:
Upvotes: 2
Views: 17641
Reputation: 14361
The way you refer to range is rather odd.. because you missed out range
reference. Oddly enoughbthat you do it correct on the next line at
Debug.Print Application.Match(Range("IdSelect").Value, Worksheets("Input").Range("ProjectList"), 0)
So try this please: (it take me 100 years to format my own post on mobile.....). Make sure to use explicit reference as shown in my sample code below. Set your sheets accordingly.
Dim ws as Worksheet
Set ws = Sheets(1)
IsError(Application.Match(ws.Range("IdSelect").Value, ws.Range("ProjectList"), 0)) Then
And here is for you to read on for error handling on on match
.
Upvotes: 8