Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何避免重复n次相同的公式块 解释_Excel_Excel Formula_Excel 2013 - Fatal编程技术网

Excel 如何避免重复n次相同的公式块 解释

Excel 如何避免重复n次相同的公式块 解释,excel,excel-formula,excel-2013,Excel,Excel Formula,Excel 2013,我发现我的问题对于我现在正在做的事情和我过去做过的事情来说都很常见 我正在编写一个Excel公式,对于列查找值中的给定输入,将扫描10个不同的Excel文件,以使用匹配和索引函数返回一个值 [A] [B] Lookup value Returned value 123 val 列返回值具有以下公式(草图): 其基本思想是从一行中的第一列中查找一个值,其中给定的值(输入)存在于文件中的任意两个表中并使用不同的文件名执行完全

我发现我的问题对于我现在正在做的事情和我过去做过的事情来说都很常见

我正在编写一个Excel公式,对于列
查找值
中的给定输入,将扫描10个不同的Excel文件,以使用
匹配
索引
函数返回一个值

    [A]              [B]
Lookup value    Returned value
    123              val
返回值
具有以下公式(草图):

其基本思想是从一行中的第一列中查找一个值,其中给定的值(输入)存在于文件中的任意两个表中并使用不同的文件名执行完全相同的操作10次

我正在使用
IFNA
函数继续运行,直到在任何文件中找到一个值或处理完所有文件

问题:
不必为
File1中的每个文件重复给定的草图10次。。。File10
我如何更简单地处理这个问题,比如不重复代码10次?越简单越好。

我建议用VBA来代替。然后您可以轻松地循环浏览文件,例如:

Function FindValue()

    Dim fileNames
    fileNames = Array("FileName1", "FileName2", "FileName3")


    For i = 1 To UBound(fileNames)
        'Generic code for finding value using fileName(i)
    Next i
End Function
更新:

这似乎对我有用。不过,错误处理可能做得更好。如果没有匹配项,可能需要根据您想要做的事情来改变行为

Function FindValue(LookupString)

    Dim fileNames
    Dim tempRange
    Dim tempRange3

    fileNames = Array("VBATesting2.xlsm", "VBATesting3.xlsm")
    On Error GoTo ErrorHandler

    For i = 0 To UBound(fileNames)
        tempRange = Workbooks(fileNames(i)).Worksheets("Sheet1").Range("A:B").Find(LookupString).Offset(, 1)
        If tempRange <> "" Then
            FindValue = tempRange
            Exit Function
        Else
            tempRange = Workbooks(fileNames(i)).Worksheets("Sheet2").Range("A:B").Find(LookupString).Offset(, 1)
            If tempRange <> "" Then
                FindValue = tempRange
                Exit Function
            Else
                FindValue = "No Match"
            End If
        End If
    Next i

    ErrorHandler:
    tempRange = ""
    Resume Next
End Function
函数FindValue(LookupString)
模糊文件名
微弱温度范围
暗温度范围3
fileNames=Array(“VBATesting2.xlsm”、“VBATesting3.xlsm”)
关于错误转到错误处理程序
对于i=0到UBound(文件名)
tempRange=工作簿(文件名(i))。工作表(“Sheet1”)。范围(“A:B”)。查找(LookupString)。偏移量(,1)
如果温度范围为“”,则
FindValue=tempRange
退出功能
其他的
tempRange=工作簿(文件名(i))。工作表(“Sheet2”)。范围(“A:B”)。查找(LookupString)。偏移量(,1)
如果温度范围为“”,则
FindValue=tempRange
退出功能
其他的
FindValue=“不匹配”
如果结束
如果结束
接下来我
错误处理程序:
tempRange=“”
下一步继续
端函数

您能提供一个工作示例吗?我没有标记我的问题VBA,因为我对它知之甚少。相同的查找值可能会出现多次,如果是,它们将始终相同,即返回第一个匹配的值(如果有匹配)是否可以?它可能会出现多次,是的-如果至少有一个匹配,它将返回第一个匹配的值。
Function FindValue(LookupString)

    Dim fileNames
    Dim tempRange
    Dim tempRange3

    fileNames = Array("VBATesting2.xlsm", "VBATesting3.xlsm")
    On Error GoTo ErrorHandler

    For i = 0 To UBound(fileNames)
        tempRange = Workbooks(fileNames(i)).Worksheets("Sheet1").Range("A:B").Find(LookupString).Offset(, 1)
        If tempRange <> "" Then
            FindValue = tempRange
            Exit Function
        Else
            tempRange = Workbooks(fileNames(i)).Worksheets("Sheet2").Range("A:B").Find(LookupString).Offset(, 1)
            If tempRange <> "" Then
                FindValue = tempRange
                Exit Function
            Else
                FindValue = "No Match"
            End If
        End If
    Next i

    ErrorHandler:
    tempRange = ""
    Resume Next
End Function