【问题标题】:How to call worksheet function from word如何从word中调用工作表函数
【发布时间】:2015-07-06 08:02:46
【问题描述】:

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

【问题讨论】:

    标签: excel vba ms-word worksheet-function


    【解决方案1】:

    我先回答你提出的问题:

    如何从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
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 2016-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多