简体   繁体   中英

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.

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. 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.

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?

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. The Cell is the current row and Column A The offset is to post values in corresponding Columns if the criteria is met.

Hopefully someone can assist.

Regards Alan

You can't use IfError() like that in VBA, instead something like:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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