简体   繁体   English

Excel / VBA:使用公式查找范围内的常数

[英]Excel /VBA : find constants in range with a formula

I am trying to setup a formula in excel/vba that will allow me to input a range of cells and output in one cell whether or not there are any constants in that range. 我正在尝试在excel / vba中设置一个公式,该公式将允许我输入一个单元格范围并在一个单元格中输出,无论该范围内是否有常量。 Its real easy to do for one cell but I cannot seem to get something working for the range. 对于一个单元格,它确实很容易做到,但是我似乎无法在该范围内工作。 Any ideas? 有任何想法吗?

thanks a lot, Jeremy 非常感谢,杰里米

Code for UDF that will return True for cells with a value and no formula: UDF的代码,对于具有值和没有公式的单元格将返回True

Option Explicit

Public Function DetectConstantInRange(rng As Range) As Boolean

    Dim rngCell As Range
    Dim blnResult As Boolean

    'assume false
    blnResult = False

    'iterate each cell in range
    For Each rngCell In rng
        'cell with value and no formula is constant
        If Not rngCell.HasFormula And rngCell.Value <> vbEmpty Then
            blnResult = True
            'at least one constant so exit
            Exit For
        End If
    Next rngCell

    'return result        
    DetectConstantInRange = blnResult

End Function

Start here: 从这里开始:

http://www.excel-easy.com/vba/examples/loop-through-defined-range.html http://www.excel-easy.com/vba/examples/loop-through-defined-range.html

you need to decide which range you want to use your function on, define it,add a loop to loop through all cells in range and apply your function. 您需要确定要在哪个范围上使用函数,对其进行定义,添加一个循环以遍历范围中的所有单元格并应用您的函数。

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

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