excel - 是否可以使用创建的函数从另一张表中获取特定数据?
问题描述
所以,我试图用这个概念创建一个 Excel 函数:根据用户输入的货币代码,搜索代码并从另一个工作簿的工作表中获取对应的汇率值。
例子:
User input in cell: =xrate(220) '220 being the code for dollar
Function returns: 5.000 'dollar exchange rate present in second workbook
我对在 VBA 中创建函数知之甚少,但尝试了以下方法:
Function xrate(code as Long)
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
Set wb1 = ThisWorkbook 'wb for the function
Set ws1 = wb1.Sheets(1)
Set wb2 = Workbooks.Open("C:\User\Test\rates.xlsm") 'wb with the rates
Set ws2 = wb2.Sheets("Rates")
ws2.Activate
'Column B is where the code is at and Column F has the correspondent Exchange Rate
For i = 1 to 156 '156 being the last row with data
If ws2.Cells(i,"B").Value = code then
xrate = Cells(i,"F").Value
End if
Next i
End Function
我已经测试了与“Sub”相同的代码并且它工作正常,但作为一个函数它返回我#VALUE!错误,似乎什么也没做。
带有费率的表格不能与将使用该功能的工作簿位于同一工作簿中,因为该表格必须每天更新,并且用户总是让他们的工作簿被使用锁定。
有可能做这样的事情吗?
谢谢!
解决方案
推荐阅读
- sql - 级联删除时的 SQL Server 死锁
- angular - 在角度 7 中更改父组件中的变量
- php - PHP 会话在多个网站上不可用
- reactjs - React/Redux 排序功能未更新状态
- scilab - Scilab“加载”命令导致布尔值
- azure - 遍历 Set-Header 策略中的多个值
- c# - ReadOnlyObservableCollection
在 WPF 设计时数据中 - build - bitbucket 管道构建实例的默认实例规范是什么,例如 CPU、Ram 等,我们可以配置它吗?
- jenkins - 通过詹金斯管道在神器中设置属性中的特殊字符
- r - 无法循环通过ggplot直方图