如何访问VBA中的Range对象的特定值?

好的,

所以这里是我用于统计分析的代码片段。 我需要将我的variables“Freq”定义为一个范围,因为我使用不同的Excel公式来加速代码,如AVERAGE(),STEYX(),DEVSQ(),FORECAST()等等。因为我需要快速迭代大量的数据,并且只能使用Excel公式来限制。

dim Freq as Range dim sht as Worksheet dim n as integer n = sht.Cells(sht.Rows.Count, "D").End(xlUp).Row - 1 Set sht.ThisWorkbook.ActiveSheet Set Freq = sht.Range("D" & 2 & ":D" & n + 1) 

后来在我的代码中,我想validation“Freq”variables中的任何值是高于还是低于某个值。 我知道如何通过检查范围中的每个单元格,或为该范围指定一个数组来做到这一点,但是我觉得对于同一范围的数据有第二个variables是还原和低效的。

当我查看VBA中的“Freq”对象时,我看到一个Value2(x,1),x是1到n之间的任意数字,但是我还没有find访问这个值的方法。 我试过了:

 tmp=Freq.Value(1) tmp=Freq.Value(1,1) tmp=Freq.Value2(1) tmp=Freq.Value2(1,1) 

但没有一个工作。

所以,基本上我的问题是:如何访问VBA中的Range对象的特定值?

任何帮助是极大的赞赏!

Range.Value接受一个可选参数,所以Freq.Value(1,1)只是不正​​确的语法。

为了省略可选的参数,你可以按照每个Rory的Freq.Value()(1,1)。

或者,将您的Freq.Value转换为变体数组:

 Dim myArray as Variant myArray = Freq.Value MsgBox myArray(1,1) 

ExcelRangeValueType如下所示,省略时使用默认参数值:

 xlRangeValueDefault 10 Default. If the specified Range object is empty, returns the value Empty (use the IsEmpty function to test for this case). If the Range object contains more than one cell, returns an array of values (use the IsArray function to test for this case). xlRangeValueMSPersistXML 12 Returns the recordset representation of the specified Range object in an XML format. xlRangeValueXMLSpreadsheet 11 Returns the values, formatting, formulas, and names of the specified Range object in the XML Spreadsheet format. 

相关: https : //fastexcel.wordpress.com/2017/03/13/excel-range-valuevaluetype-what-is-this-parameter/