我试图让我的函数在另一张表的表中查找值。
表格的行用数字标记,但列用字符串命名。
这是我的功能代码
Function GetValue(row As String, col As String)
Dim ws As Worksheet
Dim ValueSearch As Range
Set ws = Sheet2
Set ValueSearch = ws.Range("Wheels")
GetValue = ValueSearch.Cells(row, col)
End Function
我的 table 在第 2 页上,名为 WheelsWheelSize NoTyre Budget Premium
16 150 200 240
17 180 220 265
18 215 245 310
19 255 360 460
20 340 550 600
我有 2 个带有 WheelSize 编号和轮胎类型的组合框,我希望使用这些组合框来交叉引用以最终得出价格
最佳答案
我将您的数据转换为表格。
这是可以解决您的问题的代码:
Public Function GetValue(row As String, col As String)
GetValue = Range("Wheels").Cells(Range("Wheels[WheelSize]").Find(row).row - 1, Range("Wheels[#Headers]").Find(col).Column)
End Function
我不知道你为什么要使用 VBA 来完成这项任务。你可以使用这个公式:=INDEX(Wheels,MATCH(G2,Wheels[WheelSize],0),MATCH(G3,Wheels[#Headers],0))
其中 G2 是车轮尺寸,G3 是轮胎类型。在附件中,您还可以看到 VBA 解决方案和公式。
https://gofile.io/d/nnT7nc
如果您需要更多说明,请写评论。
关于excel - 使用字符串作为输入时 GetValue 函数中的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66614536/