我正在尝试从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/