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

标签 excel vba ms-word worksheet-function

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

相关文章:

excel - VBA在集合中按值获取键

excel - Octave:无法将可选参数传递给 VBA 函数(通过 COM 服务器添加 excel 表时)

VBA If Not Or 语句

MS word 的 VBA

vb.net - 如何在 vb.net 的 MVC 4 中创建并返回带有 Controller 的 Excel 文件?

sql - 将一百万条记录从 excel 复制到数据库

excel - Excel 中对 Sharepoint 上其他工作簿的单元格引用/公式不会更新值

vba - 从变量引用另一个工作簿中的工作表时下标超出范围

c#如何将光标设置在插入文本之前的末尾

c# - 打开 XML(Microsoft Word): C# Table Justification