gpt4 book ai didi

excel - 如何在word中调用工作表函数

转载 作者:行者123 更新时间:2023-12-02 22:52:36 24 4
gpt4 key购买 nike

我正在尝试从Word中查找Excel中的最大列,之前我已经使用过

MyResult = application.Worksheetfunction.max (Range("B4:B7"))

从 Excel 开始,现在我需要在 Word 中通过 VBA 进行类似的操作。

但是我无法弄清楚如何从 word 中执行此操作,我是否需要创建自己的 for 循环并创建自己的 max 函数?

maxVal =0
for i=2 to lastrow
if xlapp.ws.cells(i,2)>maxVal then
maxVal= xlapp.ws.cells(i,2)
end if
next i

最佳答案

我先回答你提出的问题:

How to call worksheet function from word

像这样:

 Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
maxVal = excelApp.Worksheetfunction.max(1, 2, 3) 'Some values separated by comma or an array
excelApp.Quit

现在您需要的只是 Max 函数。就像这样:

Function MaxFunc(arr)
Dim maxVal, tmpVal, ifFirst
ifFirst = True
For Each it In arr
If ifFirst Then
maxVal = it: ifFirst = False
Else
If maxVal < it Then maxVal = it
End If
Next
MaxFunc = maxVal
End Function

关于excel - 如何在word中调用工作表函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com