如何用VBA获取Application.WorksheetFunction.Index/Match输出的单元格地址?
获取VBA表达式对应单元格的地址
已知工作表中用于获取指定单元格地址的公式如下:
=CELL("address",INDEX(Motor.Table, MATCH(R26,Motor.Table.RefID,0),MATCH("EPAct",Motor.Eff.Row,0)))
该公式通过INDEX+MATCH组合定位到Motor.Table区域内符合条件的单元格,再借助CELL("address")提取该单元格的地址。
现在需要对以下VBA表达式的结果,获取其对应的单元格地址:
Application.WorksheetFunction.Index(rngMotorTable, _ Application.WorksheetFunction.Match(strMotorData,rngRefId,0), _ Application.WorksheetFunction.Match(strMotorEffType,rngMotorEffRow,0))
实现方法
上述VBA代码里的Index函数返回的是一个Range对象,直接调用该对象的.Address属性就能拿到对应的单元格地址。修改后的VBA代码示例如下:
Dim targetCell As Range Set targetCell = Application.WorksheetFunction.Index(rngMotorTable, _ Application.WorksheetFunction.Match(strMotorData, rngRefId, 0), _ Application.WorksheetFunction.Match(strMotorEffType, rngMotorEffRow, 0)) ' 获取单元格地址(可按需指定参数) Dim cellAddress As String cellAddress = targetCell.Address ' 返回不带工作表名的地址,例如$A$1 ' 若需要包含工作表名,使用以下写法: ' cellAddress = targetCell.Address(External:=True)
对应关系说明
rngMotorTable对应工作表公式中的Motor.Table区域strMotorData对应工作表公式中的R26单元格值rngRefId对应工作表公式中的Motor.Table.RefID区域strMotorEffType对应工作表公式中的"EPAct"rngMotorEffRow对应工作表公式中的Motor.Eff.Row区域
内容的提问来源于stack exchange,提问作者Wayne V
相关产品推荐
相关产品推荐

