Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 将公式应用于整个范围_Excel_Range_Formula_User Defined_Vba - Fatal编程技术网

Excel 将公式应用于整个范围

Excel 将公式应用于整个范围,excel,range,formula,user-defined,vba,Excel,Range,Formula,User Defined,Vba,对于经过实验的VBA开发人员/用户来说,这可能是一段非常简单的代码,但是我是一名新编程人员,我已经在这项任务上坚持了几天了:(.我所需要的是将一个公式应用于用户定义的范围,或者作为一个整体(如果可能的话),或者使用“for each next”或“for next”循环使用它我每次尝试都会犯错误。有人能帮我解决这个问题吗?…非常提前 假设这个公式很简单,比如F=m*a,在我选择的范围内是“m” 此处显示选择范围的代码: Option Base 1 Sub KVmodel()

对于经过实验的VBA开发人员/用户来说,这可能是一段非常简单的代码,但是我是一名新编程人员,我已经在这项任务上坚持了几天了:(.我所需要的是将一个公式应用于用户定义的范围,或者作为一个整体(如果可能的话),或者使用“for each next”或“for next”循环使用它我每次尝试都会犯错误。有人能帮我解决这个问题吗?…非常提前

假设这个公式很简单,比如F=m*a,在我选择的范围内是“m”

此处显示选择范围的代码:

Option Base 1


    Sub KVmodel()

    'Select the data points from the excel spreadsheet

    Dim A, B As Range
    Set A= Application.InputBox("Select A Range", "Select a range", Type:=8)
    Set B= Application.InputBox("Select B Range", "Select a range", Type:=8)


    'Verify the lenght of the selected data
    If A.Count = B.Count Then
    MsgBox "Do you want to run the KV Model Adjustment?", vbYesNo


    'Calculates F according to the values of the model

    -
    -
    -


    Else
    MsgBox "The range sizes are different, please re-select the input data"

    End If

    End Sub
请尝试以下代码:

Option Base 1


Sub KVmodel()

    On Error Resume Next

    'Select the data points from the excel spreadsheet

    Dim A As Range, B As Range
    Set A = Application.InputBox("Select A Range", "Select a range", Type:=8)
    Set B = Application.InputBox("Select B Range", "Select a range", Type:=8)

    Dim userDefinedRng As Range ' assumption
    Set userDefinedRng = Range("A1:A10")

    On Error GoTo 0

    If Not A Is Nothing And Not B Is Nothing Then

        'Verify the lenght of the selected data
        If A.Count = B.Count Then
            retVal = MsgBox("Do you want to run the KV Model Adjustment?", vbYesNo)

            If retVal = vbYes Then
                'Calculates F according to the values of the model
                userDefinedRng.FormulaR1C1 = "= 1 * 2" ' here it applies forumla to whole userdefined range
                MsgBox "yes"
            Else
                MsgBox "no"
            End If

        Else
            MsgBox "The range sizes are different, please re-select the input data"

        End If
    End If

    Exit Sub

End Sub

你想在每个单元格中插入一个公式还是在VBA中插入一个公式的结果?你给出的公式示例,你说m是什么,a是什么?m也是整个范围吗?还是其中的一个单元格,a也是一样?哦,很抱歉不能更精确,“a”这意味着一个标量,整个想法是生成另一个范围或数组,作为应用于所选范围的公式的结果。PS:thxs作为提示答案;)那么您有3个范围?A、 B和M(你选择的),你想用A和B做点什么,然后把结果放在M中?或者你有两个范围,A和B,对A做点什么,然后把结果放在B中?我有范围A或B(所选的一个),我应该生成第三个范围F,作为公式的结果。我试图用F=m*A而不是实际情况来简化问题,似乎我只是让它更难理解:/All抱歉。感谢你的帮助2063626。至少它运行时没有错误,但我得到的是一个从A1到A10的数字2的固定一维向量。位置不是问题,很容易修改,问题是值:我尝试了If retVal内的不同输入,也尝试了不同的选择范围,它一直给我一个unidim。2的向量,不管我选择什么:(thx)。