简单的VBA函数可返回范围值的乘积 [英] Simple VBA function that returns product of range value

查看:82
本文介绍了简单的VBA函数可返回范围值的乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望函数将一系列单元格作为参数并返回其乘积.让我们为单元格假定以下值:

I want the function to take a range of cells as an argument and return their product. Let's assume the following value for cells:

A1=5 
A2=2 
A3=3 

让我们调用函数 Multiply . = Multiply(A1:A3)将返回30(= 5×2×3).

Let's call the function Multiply. =Multiply(A1:A3) will return 30 (=5×2×3).

这是什么代码?我只是想使自己熟悉语法,这将对您有很大帮助.

What is the code for this? I'm just trying to familiarize myself with the syntax and this will help out a lot.

已解决:

Function multiply(rng As Range)

    multiplied = 1

    For Each cell In rng
        multiplied = multiplied * cell.Value
    Next

    multiply = multiplied

End Function

推荐答案

您可以直接使用 PRODUCT VBA 版本,即

You can use the VBA version of PRODUCT directly, ie

MsgBox WorksheetFunction.Product([a1:a3])

这篇关于简单的VBA函数可返回范围值的乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆