简体   繁体   English

Excel公式转换为VBA代码错误

[英]Excel Formula into VBA code error

I am getting a few errors with VBA code that I have written to convert from a formula based query. 我在编写VBA代码以从基于公式的查询转换时遇到了一些错误。

Cell.Offset(0, 37).Value = Application.WorksheetFunction.IfError(Application.WorksheetFunction.Index((Sheets("CP INFO").Range("N6:AK570")), Application.WorksheetFunction.Match(Cell.Offset(0, 36).Value, Sheets("CP INFO").Range("N6:AK570"), 0), 1), "No SFH")

The error is stating unable to get the 'Match property of the worksheetfunction class. 该错误表明无法获取worksheetfunction类的'Match属性。 The original formula for the above code is - =IFERROR(INDEX('CP INFO'!$N$6:AK$570,MATCH(VALUE(AK4),'CP INFO'!$N$6:$N$570,0),1),"NO SFH") SO I cannot see what is wrong with it. 上面代码的原始公式是- =IFERROR(INDEX('CP INFO'!$N$6:AK$570,MATCH(VALUE(AK4),'CP INFO'!$N$6:$N$570,0),1),"NO SFH")所以我看不到这是怎么回事。

The next error I am getting is with the below code. 我遇到的下一个错误是以下代码。

Cell.Offset(0, 39).Value = Application.WorksheetFunction.IfError(Left(Application.WorksheetFunction.IfError(Left(Right(Cell.Offset(0, 24), _
Len(Cell.Offset(0, 24)) - Application.WorksheetFunction.Find("(", Cell.Offset(0, 24))), (Len(Cell.Offset(0, 24)) - Application.WorksheetFunction.Find("-", Cell.Offset(0, 24))) + 1), ""), _
Application.WorksheetFunction.Find("-", Application.WorksheetFunction.IfError(Left(Right(Cell.Offset(0, 24), Len(Cell.Offset(0, 24)) - _
 Application.WorksheetFunction.Find("(", Cell.Offset(0, 24))), (Len(Cell.Offset(0, 24)) - Application.WorksheetFunction.Find("-", Cell.Offset(0, 24))) + 1), "")) - 1), _
Application.WorksheetFunction.IfError(Left(Right(Cell.Offset(0, 24), Len(Cell.Offset(0, 24)) - _
Application.WorksheetFunction.Find("(", Cell.Offset(0, 24))), (Len(Cell.Offset(0, 24)) - Application.WorksheetFunction.Find("-", Cell.Offset(0, 24))) + 1), ""))

This error is relating to the find function stating unable to find the property. 此错误与查找功能声明无法找到属性有关。 I'm a real novice when it comes to these functions within VBA so no doubt I am doing something wrong or missing something? 关于VBA中的这些功能,我是一个真正的新手,所以毫无疑问,我做错了什么或缺少了什么?

Original formula this code has come from. 此代码来自的原始公式。

=IFERROR(LEFT(IFERROR(LEFT(RIGHT(DATA2!Y4,LEN(DATA2!Y4)-FIND("(",DATA2!Y4)),(LEN(DATA2!Y4)- 
FIND("-",DATA2!Y4))+1),""),FIND("-",IFERROR(LEFT(RIGHT(DATA2!Y4,LEN(DATA2!Y4)- 
FIND("(",DATA2!Y4)),(LEN(DATA2!Y4)-FIND("-",DATA2!Y4))+1),""))-1),IFERROR 
(LEFT(RIGHT(DATA2!Y4,LEN(DATA2!Y4)-FIND("(",DATA2!Y4)),(LEN(DATA2!Y4)-FIND("-",DATA2!Y4))+1),""))

The design of this macro is to loop through roughly 3-5k rows of data and produce a report of data pre sorted. 此宏的设计是循环遍历大约3-5k的数据行,并生成预排序数据的报告。 The Cell is the current row and Column A The offset is to post values in corresponding Columns if the criteria is met. 单元格是当前行,是列A。如果满足条件,则偏移量将值张贴在相应的列中。

Hopefully someone can assist. 希望有人可以提供帮助。

Regards Alan 问候阿伦

You can't use IfError() like that in VBA, instead something like: 您不能像在VBA中那样使用IfError() ,而应像这样:

v = "No SFH"
On Error Resume Next
    With Application.WorksheetFunction
        v = .Index((Sheets("CP INFO").Range("N6:AK570")), .Match(Cell.Offset(0, 36).Value, Sheets("CP INFO").Range("N6:AK570"), 0), 1)
    End With
On Error GoTo 0
Cell.Offset(0, 37).Value = v

NOTE: 注意:

As Scott points out, the arguments of MATCH() also require correction. 正如Scott所指出的, MATCH()的参数也需要更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM