VBA application.match 错误 2015

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14188010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:04:26  来源:igfitidea点击:

VBA application.match error 2015

vbaexcel-vbaexcel

提问by Matthias

In my Mainprocedure 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.

在我的Main程序中,我想编写一个快速的 if 语句来检查用户是否进行了有效的输入(用户从数据列表中选择项目数,请参见附加的屏幕截图)。为此,我正在检查项目编号是否不在项目列表中。如果是这样,则会显示错误消息;如果不是,则调用许多其他过程。

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?

出于某种原因,虽然我在运行时收到错误 2015,这意味着 if 语句始终为真,即使在正确的用户条目上也是如此。有人可以帮我理解错误吗?

  • The project number input is a named cell called "IdSelect" and is on a sheet called "Invoice"
  • The data against which this input is checked is on a sheet called "Input"
  • The data is stored in column B and called "ProjectList"
  • 项目编号输入是名为“IdSelect”的命名单元格,位于名为“Invoice”的工作表上
  • 检查此输入的数据位于名为“输入”的工作表上
  • 数据存储在 B 列并称为“ProjectList”

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!)

下面的代码(注意:我已经尝试将其粘贴 5 次,但由于某种原因,这次格式仍然无法正常工作 - 知道这可能是什么吗?代码格式正确。抱歉显示混乱;如果有人能告诉我有什么问题我将不胜感激!)

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:

“输入”表的屏幕截图:

enter image description here

enter image description here

回答by bonCodigo

The way you refer to range is rather odd.. because you missed out rangereference. Oddly enoughbthat you do it correct on the next line at Debug.Print Application.Match(Range("IdSelect").Value, Worksheets("Input").Range("ProjectList"), 0)

你提到范围的方式很奇怪......因为你错过了range参考。奇怪的是,你在下一行做对了 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 referenceas shown in my sample code below. Set your sheets accordingly.

所以请试试这个:(我花了 100 年的时间在手机上格式化我自己的帖子......)。确保使用显式引用,如下面的示例代码所示。相应地设置你的床单。

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.

这是供您继续阅读有关错误处理的内容match