我有一个包含我们所有部分的 Access 数据库。我正在制作一个 Excel 插件供用户使用。他们可以通过执行以下操作(在任何 Excel 单元格中)来查询零件:
=MUN("VOLTAGE",A3)
此函数将查询 Access 数据库并找到与 PARTNO 值对应的 VOLTAGE 值(在这种情况下,PARTNO 值在用户 Excel 文件的单元格“A3”中)。
当我开始创建插件时,我只在 Excel 中测试所有内容(我的数据库在 Excel 中),我很快意识到这很愚蠢。
这是我的 VBA,仅在 EXCEL 中:
Function MUN(what, partNo)
Dim ws As Worksheet, aCell As Range, pos As String, tablex As String, LastColumn As Integer, LastRow As Long, LastCell As Range, LastC As String, LastR As Integer
p = "P:\Engineering\Programs"
f = "partsDB.xls"
s = "Sheet1"
path = "'" & p & "[" & f & "]" & s & "'!"
Set ws = ActiveWorkbook.Sheets("table")
'DETERMINES THE LAST COLUMN AND ROW USED IN THE WORKSHEET (THIS WILL HELP DETERMINE THE TABLE FOR VLOOKUP)
LastRow = Cells.Find(what:="*", after:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(what:="*", after:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
LastC = Replace(Cells(1, LastColumn).Address(False, False), "1", "")
LastR = Cells(Rows.Count, "a").End(xlUp).Row
'DETERMINES THE COLUMN POSITION OF THE "WHAT"
Set aCell = ws.Rows(1).Find(what:=UCase(what), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
pos = aCell.Column
'SETS THE TABLE
tablex = "A$1:" & LastC & "$" & LastR
MUN = Application.WorksheetFunction.VLookup(partNo, Range(tablex), pos, False)
End Function
我遇到了另一个问题。我需要用多个参数做一个 VLOOKUP。我需要做一个 VLOOKUP,其中 XX = "XXX" 和 YY = "YYY" 然后返回 "ZZZ"。
由于 MS Access 可以使用 SQL 查询,我如何连接到 Access 数据库并执行将复制上述函数中的内容的查询,以及使用多个参数执行 VLOOKUP?