我们中许多人已经注意到使用Excel时可以使用一些非常有用的功能,但是这些相同的功能作为标准功能在Access中不可用。
我最近遇到的一个特定问题(我也知道其他人最近也遇到了这个问题)正在四舍五入。 我知道Excel ROUNDUP()函数舍入为零而不是向上取整,但是在大多数情况下仍然有用。
要允许Access数据库使用Excel(以及扩展的其他Office库),您需要简单地使该库可用(这是在数据库级别而不是Access安装级别):
- 打开(或切换到)VBA窗口(Alt-F11)。
- 从菜单中选择工具 / 参考...
- 查找相关的库(在这种特定情况下,它将是我在此处使用的Access 2000的Microsoft Excel 9.0对象库 ,或者适合您当前版本的Office)。
这样可以避免重现您可能已经熟悉的一堆功能,但是令您失望的是,Access本身没有这些功能。
最初设置此功能时,我做了一些不好的测试,并认为Excel函数可用于引用了Excel库的数据库中的SQL。 随后,我发现情况并非如此。
但是,Jet SQL可以访问数据库中的Public函数,因此,如果您需要实现Excel函数,则仍然可以通过创建一个封装函数(在数据库中定义为Public)来实现此目的,调用Excel函数。 数据库中的VBA代码可以访问Excel库。 只有Jet SQL不能。
张贴者
斯科特·普赖斯这是Excel函数的特定示例; 预测()。 从Access中的VBA代码模块调用。 (使用Access 2003开发)。
Public Function xlForeCast() As Double
Dim MyDate As Integer 'Will be the point for which you are forecasting, in this case 2007
Dim MyRange() As Variant 'Will be the independent element of the forecast function
Dim MyRange1() As Variant 'Will be the dependent element of the forecast function
Dim MyArray() As Variant 'Temp array to hold the query result set values before being split into the two preceding arrays
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim ls As Integer 'Temp variable to count the rows in the list
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("qryGetHistory") 'Opens the query that feeds the data
With rs1
.MoveFirst
.MoveLast
ls = .RecordCount
.MoveFirst
MyArray() = .GetRows(ls) 'Populate the temporary array with the query results
End With
'Split the required data into two arrays, drawing from columns two and three in the query/array
MyRange() = Array(CInt(MyArray(1, 0)), CInt(MyArray(1, 1)), CInt(MyArray(1, 2)), CInt(MyArray(1, 3)), CInt(MyArray(1, 4)))
MyRange1() = Array(CInt(MyArray(2, 0)), CInt(MyArray(2, 1)), CInt(MyArray(2, 2)), CInt(MyArray(2, 3)), CInt(MyArray(2, 4)))
MyDate = CInt(DatePart("yyyy", "July 30")) 'Set the Desired point to forecast for
rs1.Close
Set rs1 = Nothing 'Reset the recordset, releasing memory
Set db = Nothing
xlForeCast = Excel.WorksheetFunction.Forecast(MyDate, MyRange1, MyRange) 'Calls the Excel forecast function
Erase MyArray 'Reset the Arrays to zero, releasing memory
Erase MyRange
Erase MyRange1
End Function
From: https://bytes.com/topic/access/insights/721622-excel-functions-access