我正在寻找一个函数,它接受两个输入参数,boardtype
和subsysnum
,然后找到具有该特定组合的行索引。但是,如果subsysnum列为空,则继续。只有某些情况下会有subsysnum
值。boardtype
必须是精确匹配的。为了实现该函数,到目前为止,我已经编写了:boardtype
和subsysnum
都定义为上述字符串。调用函数时定义的column
将为3
或5
到目前为止,我已经调用了其中包含查找表的工作表,并且相信我已经找到了boardtype
的行索引,现在我只需要合并如果可以在第二列中找到subsysnum
值,则找到行组合索引,否则继续使用空白的第二列来找到查找值。
使用上表,例如我的boardtype = AX-6和我的subsysnum = WD 1234 TEST,我希望宏获取行索引9,因为subsysnum = WD 1234包含在子系统编号WD 1234 TEST中。如果subsysnum = WD 298588试验,则返回的行索引应为8,因为它包含在值中。最后,如果在第2列中找不到subsysnum值,则它应该返回AX-6的行索引7,旁边是空白单元格。
这就是我目前所尝试的,但是,我没有得到GetClock
的任何值
Function GetClock(boardtype As String, subsysnum As String, column As Long, Optional partialFirst As Boolean = False) As Variant
Dim wbSrc As Workbook, ws As Worksheet, r1 As Range, r2 As Range, board_range As Range, firstAddress As String
FunctionName = "GetClock"
Set wbSrc = Workbooks.Open("C:\Documents\LookupTable.xlsx")
Set ws = wbSrc.Worksheets("Clock")
Set r1 = ws.Columns(1)
Set r2 = ws.Columns(2)
With r1
Set board_range = r1.Find(What:=boardtype, LookAt:=xlWhole, LookIn:=xlFormulas, MatchCase:=True) ' find board type row
If Not board_range Is Nothing Then
firstAddress = board_range.Address ' save board type address
Else
ErrorMsg = ErrorMsg & IIf(ErrorMsg = "", "", "") & SectionName & ": " & "Board " & boardtype & " could not be found in lookup table" & vbNewLine
Exit Function
End If
Do While Not board_range Is Nothing
Set subsysnum_range = r2.Find(What:=subsysnum, LookIn:=xlFormulas, LookAt:=IIf(partialFirst, xlPart, xlWhole), MatchCase:=True)
GetClock = ws.cells(board_range.row, column).value
Exit Function
Set board_range = r1.Find(boardtype, board_range)
If board_range.Address = firstAddress Then
GetClock = ws.cells(Range(firstAddress).row, column).value
If GetClock = 0 Then
ErrorMsg = ErrorMsg & IIf(ErrorMsg = "", "", "") & SectionName & ": " & "lookup table missing value" & vbNewLine
End If
Exit Function
End If
Loop
End With
End Function
UPDATE:其中Column(13)
表示Data Sheet
中存储了subsysnum
的列
Function GetClock(boardtype As String, subsysnum As String, column As Long, Optional partialFirst As Boolean = False) As Double
Dim wbSrc As Workbook, ws As Worksheet, r1 As Range, r2 As Range, board_range As Range, firstAddress As String, subsysnum_range As Range, rng_board As Range, rng_subsys As Range
FunctionName = "GetExternalClock"
Set wbSrc = Workbooks.Open("C:\Documents\LookupTable.xlsx")
Set ws = wbSrc.Worksheets("Clock")
Dim wb As Workbook, dataws As Worksheet
Set wb = Workbooks("S93.xlsm")
Set dataws = wb.Worksheets("Data Sheet")
Set r1 = ws.Columns(1)
Set r2 = ws.Columns(2)
With r1
Set board_range = r1.Find(What:=boardtype, LookAt:=xlWhole, LookIn:=xlFormulas, MatchCase:=True) ' find board type row
If Not board_range Is Nothing Then
firstAddress = board_range.Address ' save board type address
Else
ErrorMsg = ErrorMsg & IIf(ErrorMsg = "", "", "") & SectionName & ": " & "Board " & boardtype & " could not be found in lookup table" & vbNewLine
Exit Function
End If
Dim subsys As Range, cell As String
Do While Not board_range Is Nothing ' while board type is not nothing look for value of cell in column 2
For Each subsys In Range("B3:B12")
cell = subsys.value
Set subsys_rng = dataws.Columns(13).Find(What:=cell, LookIn:=xlFormulas, LookAt:=IIf(partialFirst, xlPart, xlWhole), MatchCase:=True)
If cell = "" Then
GoTo Skip
Else
GetClock= ws.cells(subsys_rng.row, column).value
End If
Skip:
Next subsys
Exit Function
'if intersect.value does not equal sysnum, then it will set board_range below only after it has checked every matching cell in column 1
Set board_range = r1.Find(boardtype, board_range)
If board_range.Address = firstAddress Then
GetClock= ws.cells(Range(firstAddress).row, column).value ' boardtype row index with empty cell in r2
If GetClock= 0 Then
ErrorMsg = ErrorMsg & IIf(ErrorMsg = "", "", "") & SectionName & ": " & "lookup table missing value" & vbNewLine
End If
Exit Function
End If
Loop
End With
Exit Function
End Function
| 主板|子系统|最小值|最大值|最小值|最大值|
| - -|- -|- -|- -|- -|- -|
| AX系列||10个|四十|10个|四百|
| AX-11突击步枪||10个|四百|10个|四百|
| AX-12突击步枪||100个|七百五十|100个|七百五十|
| AX-13突击步枪||10个|五百五十|10个|五百五十|
| AX-4突击步枪||10个|四百|10个|四百|
| AX-6突击步枪||一百二十五|五百五十|一百二十五|五百五十|
| AX-6突击步枪|WD 298588型|四十|五百个|四十|五百个|
| AX-6突击步枪|WD 1234型|一个|2个|三个|四个|
| AX-7突击步枪||一百二十五|七百五十|一百二十五|七百五十|
| AX-8突击步枪||一百二十五|五百五十|一百二十五|五百五十|
2条答案
按热度按时间uyto3xhc1#
请检查下一个函数:
可通过以下方式对其进行测试:
如果使用
column
参数,并且返回值可能是字符串,则函数返回值必须从Long
更改为Variant
...现在,如果没有匹配,则返回零(
0
)。请在测试后发送一些反馈
rm5edbpk2#
数组上的简单循环更易于管理,沿着在调用之间缓存查找表: