简体   繁体   English

Excel VBA-LCM-未定义子函数

[英]Excel VBA - LCM - Sub or Function not defined

The following code: 如下代码:

Sub lcm_()

    If Selection.Areas.Count <> 1 Then Exit Sub

    Dim a As Range
    Set a = Selection.Areas(1)
    a.Item(1).Value = Lcm(a.Item(1).Value, a.Item(2).Value)

End Sub

produces the error: Compile error: Sub or Function not defined , and I cannot figure out why. 产生错误: Compile error: Sub or Function not defined ,我无法弄清原因。 I have looked at similar problems, and it seems like the issue is that Excel cannot find the builtin function LCM, but the documentation makes it seem like it doesn't require any special libraries. 我已经看过类似的问题,看来问题在于Excel无法找到内置函数LCM,但文档使它看起来似乎不需要任何特殊的库。 I am very new to VBA, so it is entirely possible that this is something very simple, but I cannot figure out what. 我是VBA的新手,所以这很可能很简单,但是我不知道是什么。

EDIT: 编辑:

It is worth noting that if I simply click on a cell and type =LCM(A8,A9) , then it works fine. 值得注意的是,如果我仅单击一个单元格并键入=LCM(A8,A9) ,那么它将正常工作。

Try this: 尝试这个:

Sub lcm_()

    If Selection.Areas <> 1 Then Exit Sub

    Dim a As Range
    Set a = Selection.Areas(1)
    a.Item(1).Value = WorksheetFunction.Lcm(a.Item(1).Value, a.Item(2).Value)

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM