我正在尝试使用以下代码:
使用工作簿中的工作表中的三个因子(因子A,因子B和因子C)(mybook,mybook,respcetively),
在第二个工作簿(源表和源码表)中搜索工作表中的数据集,
并返回一个索引,该索引对应于源表数据中与mysheet中的三个因子匹配的数据行。
索引返回变量v,如果找不到与三个标准匹配的行,则应返回错误代码2042.
我从更大的副功能中提取了代码,并将其简化为非特定的。
Sub enter_data()
Dim mybook As Workbook, sourcebook As Workbook
Dim mysheet As Worksheet, sourcesheet As Worksheet
Dim FactorA$, FactorB$, FactorC$
Dim i%
Dim v As Variant
'set names for workbooks and worksheets
Set mybook = Workbooks("mydatabook.xlsm")
Set mysheet = mybook.Sheets("mydatasheet")
Set sourcebook = Workbooks("sourcedatasbook.xlsm")
Set sourcesheet = sourcebook.Sheets("sourcedatasheet")
'i is incremented and used to go through every line of data I want to extract from -
'I have only included one instance of i for this example
i = 3
'create names for search factors
FactorA = "'[" & mybook.Name & "]" & mysheet.Name & "'!" & mysheet.Cells(i, 1).Address(False, False)
FactorB = "'[" & mybook.Name & "]" & mysheet.Name & "'!" & mysheet.Cells(i, 6).Address(False, False)
FactorC = "'[" & mybook.Name & "]" & mysheet.Name & "'!" & mysheet.Cells(i, 3).Address(False, False)
Debug.Print " FactorA: " & FactorA, vbCrLf & _
" FactorB: " & FactorB, vbCrLf & _
" FactorC: " & FactorC, vbCrLf
'search for data
'two factor search
v = sourcesheet.Evaluate("MATCH(" & FactorA & "&" & FactorB & "&" & FactorC & ", G6:G6000&C6:C6000&D6:D6000, 0)")
Debug.Print v
'three factor search
v = sourcesheet.Evaluate("MATCH(" & FactorA & "&" & FactorB & ", G6:G6000&C6:C6000, 0)")
Debug.Print v
'three factor search using explicit references (this was copied almost directly from a cell in excel
v = sourcesheet.Evaluate("MATCH('[mydatabook.xlsm]mydatasheet'!A3&'[mydatabook.xlsm]mydatasheet'!F3&'[mydatabook.xlsm]mydatasheet'!C3, C6:C6000&D6:D6000&G6:G6000, 0)")
Debug.Print v
End Sub
我正在使用的表格的列如下:
mybook.mysheet:
因子A列B因子C列D列E因子B
sourcebook.sourcesheet:
A栏B栏因子B因子C栏E栏F因子A
目前:
在第一个debug.print v上,如果存在行,我会得到一个索引,如果不存在则得到错误2042
在第二个debug.print v上,我总是收到错误2015
在第三个debug.print v上,我总是收到错误2015
第三个debug.print中的函数直接从excel中的单元格复制,在那里它返回一个索引或#N / A
有没有理由为什么第二个和第三个debug.print不会像它应该那样返回索引或错误2042?
感谢您的帮助!