如何在FormulaR1C1中使用variables?

我有一个公式来获得最接近的价值,即,

=SMALL(R1C1:R1C144,COUNTIF(R1C1:R1C144,""<"" &8)+1 ) 

会给出最接近8的值。

我想用variables而不是直接数字“(8)”

请帮帮我。 这是我现在的代码

 Private Sub CommandButton1_Click() Dim col As Integer col = Me.TextBox1.Value ActiveCell.FormulaR1C1 = "=SMALL($A$1:$EN$1,COUNTIF($A$1:$EN$1," < "&col)+1)" End Sub 

如果你想在Excel中做到这一点与VBA分开,那么你并不需要一个variables。 你应该把你想要的任何值放在一个单独的单元格中(在这个例子中的单元格A5),然后在你的公式中引用这个单元格,像这样:

 =SMALL(R1C1:R1C144,COUNTIF(R1C1:R1C144,""<"" & A5)+1 ) 

我不相信你会想要一个VBA的解决scheme,因为你的algorithm到目前为止都是在Excel语法。 但是,如果你真的想在VBA中做到这一点,你需要findVBA语法的algorithm,并声明一个像这样的variables:

 Dim myNumber As Integer myNumber = 5 'put whatever number you want here 
 Private Sub CommandButton1_Click() Dim col As Integer col = Me.TextBox1.Value 'EDIT: .FormulaR1C1 should be .Formula ActiveCell.Formula = "=SMALL($A$1:$EN$1,COUNTIF($A$1:$EN$1,""<" & col & """)+1)" End Sub