首页 > 解决方案 > 如何使此函数遍历两个范围?

问题描述

所以我一直在尝试构建一个函数来使用For loop

=IFERROR(IF(AND(N$10>=PivotTabel!$L5;N$10<=PivotTabel!$M5);"1";"0.5");"X") & 
IF(AND(N$10>=Sheet1!$L125;N$10<=Sheet1!$M125);"yes";"n") & 
IF(AND(N$10>=Sheet1!$L126;N$10<=Sheet1!$M126);"yes";"n") & 
IF(AND(N$10>=Sheet1!$L127;N$10<=Sheet1!$M127);"yes";"n")

此公式用于检查日期是否在日期期间之间(例如检查 01.01.2020 是否在 02.08.2019 和 20.02.2020 之间,如果在第一种情况下为真,我打印“1”,其他情况下我打印“是” )。正如您在上面看到的,我需要在 VBA 中编写的公式来遍历指定数量的单元格,其中一列是 $L125: $L139,另一列是$M125:$M139

我写了下面的函数,但它给了我#VALUE!

Function Date_Increment(MaxCount As Integer) As String
' This function does incrementation of date ranges
Dim Result As String
Static i As Integer
i = 125
Dim cell As Range

'MaxCount is the number times to iterate just to test 
'My idea is to use the function COUNT("L125:L139") to return the number of cells that are not blank

For Each i In MaxCount 

    '  this is the cell i want to run formula in  

    Range("GUI!D12").Formula = "=IFERROR(IF(AND(D$10>=PivotTabel!$L5;D$10<=PivotTabel!$M5);""1"";""0.5"");""X"") & IF(AND(D$10>=Sheet1!$L" & i & ";D$10<=Sheet1!$M" & i & ");""yes"";""n"")"
    Result = Range("GUI!D12").Formula
i = i + 1
Next cell
'Next j
DateRange_Increment
End Function

我的问题是:如何让这个函数遍历两个范围?或者,如果有一个仅使用公式的简单解决方案,我也会使用它。另外请检查我的功能是否有错误。

任何帮助我都会非常感激。

标签: excelvbaexcel-formula

解决方案


推荐阅读