2013-03-12 72 views
0

这对于实验VBA开发人员/用户来说可能是一段相当简单的代码,但即时新的编程Im已经停留在这个任务上几天了:(我需要的是将一个公式应用到用户定义的范围内(如果可能的话)或者用“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 
+0

你想公式插入每个单元格或插入VBA计算公式的结果呢?你给出的公式例子,你说的是什么,一个怎么样?也是在整个范围内?或者它里面的一个单元格,对于一个单元格是相同 – NickSlash 2013-03-12 22:27:12

+0

对不起,因为不是更精确,“a”意味着一个标量,并且整个想法将会生成另一个范围或数组,作为应用于所选范围的公式的结果。 PS:thxs为您的及时答案;) – 2013-03-12 22:35:31

+0

所以你有3个范围? A,B和M(你选择的),你想用A和B做些什么,然后把结果放在M中?或者你有2个范围,A和B,对A做些什么并把结果放在B中? – NickSlash 2013-03-12 22:54:45

回答

0

试试下面的代码:

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 
+0

Thxs for your help 2063626.至少它运行没有错误,但我得到的是从A1到A10的固定一维数字2向量。位置不是问题,易于修改,问题是价值:我试过差异。在If retVal内的输入以及不同的选定范围内,它一直给我一个unidim。 2无论我选择什么:(无论如何thxs的向量。 – 2013-03-13 11:46:18

+0

@Migel Varga亲切的投票,如果我的帖子是有帮助的。 – 2013-03-13 11:47:52