这是我第一次用VBA编程,我没有得到MolarVolume的任何返回值

这是我第一次用VBA编程,我没有得到MolarVolume的任何返回值,vba,excel,Vba,Excel,无论我插入什么,我都会得到一个值错误#Value!我的函数写的方式是否有问题,用于C++和Matlab。谁能告诉我我做错了什么。这是一个函数,用于根据3个不同数据集的不同值计算化合物的摩尔体积。在我看来,逻辑(请随意更正)似乎很好。只是我不习惯Visual Basic语法。如果有任何逻辑或语法相关的错误,请告诉我。一切帮助都将不胜感激。提前谢谢你 Function MolarVolume(compund As String, T As Double, P As Double) As Double

无论我插入什么,我都会得到一个值错误#Value!我的函数写的方式是否有问题,用于C++和Matlab。谁能告诉我我做错了什么。这是一个函数,用于根据3个不同数据集的不同值计算化合物的摩尔体积。在我看来,逻辑(请随意更正)似乎很好。只是我不习惯Visual Basic语法。如果有任何逻辑或语法相关的错误,请告诉我。一切帮助都将不胜感激。提前谢谢你

Function MolarVolume(compund As String, T As Double, P As Double) As Double
T = 273.15 + T
Dim Compare As String
Dim Z01 As Double  
Dim Z02 As Double
Dim Z0 As Double
Dim Tc As Double
Dim Pc As Double
Dim Z11 As Double
Dim Z12 As Double
Dim Z1 As Double
Dim Pr As Double
Dim Pr1 As Double
Dim Pr2 As Double
Dim col As Integer
Dim Row As Integer
Dim ComparePressureZ As Double
Dim CompareTempZ As Double

Set Data = Worksheets("data").Activate

Compare = Data.Cells(Row, 1)


While Compare <> compound
Row = Row + 1
Compare = Data.Cells(Row, 1)
Wend

Tc = Data.Cells(Row, 4)
Pc = Data.Cells(Row, 5)

Tr = T / Tc
Pr = P / Pc  

Tr = Round(Tr, 1)


Set Dataz0 = Worksheets("data_Z0.csv").Activate

col = 1
Row = 1


ComparePressureZ = Dataz0.Cells(1, col)
CompareTempZ = Dataz0.Cells(Row, col)

While ComparePressureZ < Round(Pr, 1)
col = col + 1
Pr1 = Dataz0.Cells(1, col)
While CompareTempZ <> Tr
Row = Row + 1
Wend
Z01 = Dataz0.Cells(Row, col)
Wend



Set Rng = Range("B1:P1")
Row = 1
col = 2


For Each cll In Rng
If cll.Value > Pr Then
col = col
Pr2 = Dataz0.Cells(1, col)
Else
col = col + 1
End If
Next


While CompareTempZ <> Tr
Row = Row + 1
Wend


Z02 = Dataz0.Cells(Row, col)


Z0 = Z01 + (Pr - Pr1) * ((Z02 - Z01) / (Pr2 - Pr1))


Set dataz1 = Worksheets("data_Z1.csv").Activate

col = 1
Row = 1

ComparePressureZ = dataz1.Cells(1, col)
CompareTempZ = dataz1.Cells(Row, col)

While ComparePressureZ < Round(Pr, 1)
col = col + 1
Pr1 = dataz1.Cells(1, col)
While CompareTempZ <> Tr
Row = Row + 1
Wend
Z11 = dataz1.Cells(Row, col)
Wend



Set Rng = Range("B1:P1")
Row = 1
col = 2


For Each cll In Rng
If cll.Value > Pr Then
col = col
Pr2 = dataz1.Cells(1, col)
Else
col = col + 1
End If
Next


While CompareTempZ <> Tr
Row = Row + 1
Wend


Z12 = dataz1.Cells(Row, col)


Z1 = Z11 + (Pr - Pr1) * ((Z12 - Z11) / (Pr2 - Pr1))


MolarVolume = Z1


End Function    
函数MolarVolume(compund作为字符串,T作为Double,P作为Double)作为Double
T=273.15+T
作为字符串进行比较
尺寸Z01为双精度
尺寸Z02为双精度
尺寸Z0为双精度
双精度Tc
Dim Pc作为双机
尺寸Z11为双精度
尺寸Z12为双精度
尺寸Z1为双精度
双重身份
将Pr1调暗为双精度
将Pr2调暗为双精度
作为整数的Dim col
将行设置为整数
Dim ComparePressureZ为双精度
Dim CompareTempZ为双精度
设置数据=工作表(“数据”)。激活
比较=数据。单元格(第1行)
而比较化合物
行=行+1
比较=数据。单元格(第1行)
温德
Tc=数据单元格(第4行)
Pc=数据单元(第5行)
Tr=T/Tc
Pr=P/Pc
Tr=圆形(Tr,1)
设置数据Z0=工作表(“数据_Z0.csv”)。激活
col=1
行=1
ComparePressureZ=Dataz0.单元格(1,列)
CompareTempZ=Dataz0.单元格(行、列)
而比较压力zPr,则
col=col
Pr2=数据Z0.单元格(1,列)
其他的
col=col+1
如果结束
下一个
而CompareTempZ Tr
行=行+1
温德
Z02=数据Z0.单元格(行、列)
Z0=Z01+(Pr-Pr1)*((Z02-Z01)/(Pr2-Pr1))
设置dataz1=工作表(“data_Z1.csv”)。激活
col=1
行=1
ComparePressureZ=dataz1.单元格(1,列)
CompareTempZ=dataz1.单元格(行、列)
而比较压力zPr,则
col=col
Pr2=数据Z1.单元格(1,列)
其他的
col=col+1
如果结束
下一个
而CompareTempZ Tr
行=行+1
温德
Z12=数据Z1.单元格(行、列)
Z1=Z11+(Pr-Pr1)*((Z12-Z11)/(Pr2-Pr1))
摩尔体积=Z1
端函数

使用F8单步执行代码。它在哪里中断?我将为您省去一个麻烦:如果您从工作表中以用户定义函数的形式调用它,那么就不能这样做。您不能使用
Activate
方法(事实上,您不能在从工作表调用的UDF中使用许多
Range
方法)。范围应作为参数传递给函数。以下是一些在UDF中也不能做的事情-感谢您的响应,如果不是UDF,我如何运行此过程?下面有任何帮助吗?使用F8逐步完成代码。它在哪里中断?我将为您省去一个麻烦:如果您从工作表中以用户定义函数的形式调用它,那么就不能这样做。您不能使用
Activate
方法(事实上,您不能在从工作表调用的UDF中使用许多
Range
方法)。范围应作为参数传递给函数。以下是一些在UDF中也不能做的事情-感谢您的响应,如果不是UDF,我如何运行此过程?下面有任何帮助吗?