0

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

假设该公式很简单,例如 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
4

1 回答 1

0

Try below code :

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
于 2013-03-13T04:38:04.967 回答