简体   繁体   English

转换Excel数组公式VBA

[英]Convert Excel array formula VBA

I'm trying to do "vlookup" with 2 different criteria(Column A and G values) using "Index" and "Match" functions. 我正在尝试使用“索引”和“匹配”功能使用2个不同的条件(列A和G值)进行“ vlookup”。

and here is the line i used for the Excel command. 这是我用于Excel命令的行。

=INDEX(Database!A:KG,MATCH(1,(Database!A:A='TempSheet'!A2)*(Database!G:G='TempSheet'!G2),0),10)

How would I do it with VBA? 我将如何使用VBA? It's keep giving me the error message "Compile error: Expected end of statement". 它一直在给我错误消息“编译错误:语句的预期结尾”。

Selection= _
    "=Index(DB.Range("A:KG"), Match(1, (DB.Range("A" = Temp.Range("A" & i).Value)) * (DB.Range("G" = Temp.Range("G" & i).Value)), 0), 10)"

Thanks 谢谢

Every time you use a spreadsheet formula inside a VBA code, you need to precede it with < Excel.WorksheetFunction. 每次在VBA代码中使用电子表格公式时,都需要在其前面加上< Excel.WorksheetFunction。 > or < Application.WorksheetFunction. >或< Application.WorksheetFunction。 >. >。

For example: 例如:

Application.WorksheetFunction.Match

instead of Match only. 而不是仅匹配。

I have had little luck getting array formulas to work correctly via VBA, and use the 'IFERROR' as a workaround like so: 我很少能通过VBA使数组公式正常工作,并使用“ IFERROR”作为替代方法,如下所示:

=IFERROR(VLOOKUP(A1, Database!A:Z,1,FALSE),VLOOKUP(B1, Database!A:Z,1,FALSE))

This function will attempt to match A1, and in case of a #VALUE error, it will match B1. 此函数将尝试匹配A1,如果出现#VALUE错误,它将匹配B1。

To get this kind of formula populated on a sheet in VBA, you can loop down your sheet using the '.formula' approach. 要在VBA中的工作表上填充这种公式,可以使用“ .formula”方法循环工作表。

' get length of source data
Dim RowCount As Long
RowCount = ThisWorkbook.Sheets("Database").Cells(Rows.Count, 1).End(xlUp).Row
' now starting from row 2 to preserve headings
For i = 2 To RowCount
    ThisWorkbook.Sheets("Summary").Cells(i, 1).Formula = "=IF(ISERROR(SEARCH(""ISO"",V" & i & ")),""Order type not supported"",""Transit"")"
    ThisWorkbook.Sheets("Summary").Cells(i, 6).Formula = "=IFERROR(TEXT(VLOOKUP(B" & i & ",Database!A:N,7,FALSE),""dd-mmm-yyyy HH:MM AM/PM""),"""")"
Next

Not exactly what you were after, but hope it helps! 并非您所追求的完全一样,但希望对您有所帮助!

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

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