如何从word中调用工作表函数

How to call worksheet function from word

我试图从 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

这是基于 AnlaystCave 输入的我的代码:

val(oXLApp.Worksheetfunction.Max( _
                xlApp.Sheets("List").Range(xlApp.Sheets("List").Cells(3, columnWp), xlApp.Sheets("List").Cells(numofrows, columnWp))))